发布到Blogger使用PHP

编程入门 行业动态 更新时间:2024-10-22 17:31:17
本文介绍了发布到Blogger使用PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在得到了PHP工作在Blogger API的一个问题。

I'm having a problem getting the Blogger API for PHP to work.

我需要的是能够发表新博文我bloggeraccount。我使用的是code从这里是谷歌API页面采取:的$c$c.google/intl/nl/apis/blogger/docs/1.0/developers%5Fguide%5Fphp.html

What I need is to be able to post a new blogpost to my bloggeraccount. The code I'm using is taken from the Google API page here : code.google/intl/nl/apis/blogger/docs/1.0/developers%5Fguide%5Fphp.html

下面是我的code:

<? require_once 'Zend/Loader.php'; Zend_Loader::loadClass('Zend_Gdata'); Zend_Loader::loadClass('Zend_Gdata_Query'); Zend_Loader::loadClass('Zend_Gdata_ClientLogin'); $user = 'name@example'; $pass = 'password'; $service = 'blogger'; $client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service, null, Zend_Gdata_ClientLogin::DEFAULT_SOURCE, null, null, Zend_Gdata_ClientLogin::CLIENTLOGIN_URI, 'GOOGLE'); $gdClient = new Zend_Gdata($client); $blogID = '7973737751295446679'; function createPublishedPost($title='Hello, world!', $content='I am blogging on the internet.') { $uri = 'www.blogger/feeds/' . $blogID . '/posts/default'; $entry = $gdClient->newEntry(); $entry->title = $gdClient->newTitle($title); $entry->content = $gdClient->newContent($content); $entry->content->setType('text'); $createdPost = $gdClient->insertEntry($entry, $uri); $idText = split('-', $createdPost->id->text); $newPostID = $idText[2]; return $newPostID; } createPublishedPost(); ?>

我得到的错误是致命错误:调用一个成员函数新条目(c)中的非对象上:\\ XAMPP \\ htdocs中\\ HelloWorld的\\ blogger2.php第21行

The error I'm getting is 'Fatal error: Call to a member function newEntry() on a non-object in C:\xampp\htdocs\HelloWorld\blogger2.php on line 21'

谁能帮助我或者给我如何发布使用PHP到Blogger工作code样?

Can anyone help me out or give me a working code sample of how to post to blogger using PHP ?

推荐答案

您 $ gdClient 变量intanciated外的 createPublishedPost 功能:

Your $gdClient variable is intanciated outside of the createPublishedPost function :

$gdClient = new Zend_Gdata($client);

在函数里,已经在它之外定义的变量默认情况下不存在。:关于这一点,你可以看看在变量范围手册的页面。

这意味着 $ gdClient 在函数内部不存在;因此,它是空;所以,不是一个对象 - 这也解释了你所得到的错误讯息

This means $gdClient doesn't exist inside the function ; hence, it is null ; so, not an object -- which explains the error message you are getting.

结果要检查自己,你可以使用

To check that by yourself, you can use

var_dump($gdClient);

在函数的开头:它可以让​​你看到它是什么类型的数据;如果它不是你愿意使用类的一个实例,这不是一个好兆头; - )

at the beginning of the function : it will allow you to see what kind of data it is ; if it's not an instance of the class you are willing to use, it's not a good sign ;-)

结果你可能想要么:

You might want to either :

  • 传递变量作为参数传递给 createPublishedPost 功能
  • 或声明为 全球 在函数内部的(这样的功能可以看到的声明外的变量)的
  • pass that variable as a parameter to the createPublishedPost function
  • or declare it as global inside the function (so the function can "see" the variable as declared outside)

第一个解决方案可能是最干净的一个,我想; - )

The first solution is probably the cleanest one, I think ;-)

结果一点题外话,你可能需要配置您的 的error_reporting 级别(也),所以你得到一个 E_NOTICE 当您使用未声明的变量 - 在这种情况下,你应该得到的,例如;-)结果的您可能还需要启用 的display_errors ,开发机器上,如果它不是已经在 - 似乎是,因为你得到了致命错误消息的

As a sidenote, you might want to configure your error_reporting level (see also), so you get an E_NOTICE when you are using a variable that is not declared -- in this case, you should have gotten one, for instance ;-) You might also want to enable display_errors, on your development machine, if it's not already on -- seems to be, as you got the Fatal error message

这似乎在一开始有点讨厌,但是,一旦你习惯了它,它真的很棒:允许检测这种东西快了很多;-)结果这也将有助于检测变量名拼写错误^^结果这让你code的方式清洁!

It might seem a bit annoying at the beginning, but, once you get used to it, it is really great : allow to detect that kind of stuff a lot quicker ;-) And it also helps detect typos in variable names ^^ And it makes you code way cleaner !

更多推荐

发布到Blogger使用PHP

本文发布于:2023-10-11 23:18:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1483117.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Blogger   PHP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!