使用 PHP 发布到 Blogger [英] Posting to Blogger using PHP
问题描述
我在使用 PHP 的 Blogger API 时遇到问题.
I'm having a problem getting the Blogger API for PHP to work.
我需要的是能够将新博文发布到我的博客帐户.我使用的代码取自此处的 Google API 页面:http://code.google.com/intl/nl/apis/blogger/docs/1.0/developers_guide_php.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 : http://code.google.com/intl/nl/apis/blogger/docs/1.0/developers_guide_php.html
这是我的代码:
<?
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.com';
$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 = 'http://www.blogger.com/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 行的非对象上的成员函数 newEntry()"
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 向博主发帖的工作代码示例?
Can anyone help me out or give me a working code sample of how to post to blogger using PHP ?
推荐答案
您的 $gdClient
变量在 createPublishedPost
函数之外被初始化:
Your $gdClient
variable is intanciated outside of the createPublishedPost
function :
$gdClient = new Zend_Gdata($client);
在函数内部,在函数外部定义的变量默认不存在.
关于这个,你可以看看变量范围 手册页.
Inside a function, the variables that have been defined outside of it don't exist by default.
About that, you can take a look at the Variable scope page of the manual.
这意味着 $gdClient
在函数内部不存在;因此,它是 null
;所以,不是一个对象——它解释了你得到的错误信息.
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 ;-)
您可能想要:
- 将该变量作为参数传递给
createPublishedPost
函数 - 或声明为
global
函数内部(这样函数就可以看到"外部声明的变量)
我认为第一个解决方案可能是最干净的解决方案;-)
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
一开始可能看起来有点烦人,但是,一旦你习惯了它,它真的很棒:可以更快地检测到那种东西;-)
它还有助于检测变量名称中的拼写错误^^
它让你的代码更简洁!
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 !
这篇关于使用 PHP 发布到 Blogger的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!