使用 PHP 发布到 Blogger

发布于 2024-08-01 12:51:35 字数 1680 浏览 4 评论 0原文

我在让 PHP 的 Blogger API 正常工作时遇到问题。

我需要的是能够将新的博客文章发布到我的博客帐户。 我使用的代码取自 Google API 页面: 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 = '[email protected]';
$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:\ 中的非对象上调用成员函数 newEntry() xampp\htdocs\HelloWorld\blogger2.php on line 21'

任何人都可以帮助我或给我一个如何使用 PHP 发布到博客的工作代码示例吗?

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

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

Here is my code :

<?
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata');
Zend_Loader::loadClass('Zend_Gdata_Query');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');

$user = '[email protected]';
$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();
?>

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'

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

做个少女永远怀春 2024-08-08 12:51:35

$gdClient 移至函数体修复了某些问题,但您还必须将 $blogID 移至函数体。

Moving the $gdClient to the function body fixed something, but you also have to move the $blogID into the function body.

枕梦 2024-08-08 12:51:35

您的 $gdClient 变量在 createPublishedPost 函数外部进行初始化:

$gdClient = new Zend_Gdata($client); 

在函数内部,默认情况下不存在在函数外部定义的变量。

关于这一点,你可以看看变量作用域 手册页。

这意味着函数内部不存在 $gdClient ; 因此,它是 null ; 所以,不是一个对象——这解释了您收到的错误消息。

要自行检查,您可以

var_dump($gdClient);

在函数的开头使用:它将允许您查看它是什么类型的数据; 如果它不是您愿意使用的类的实例,那么这不是一个好兆头;-)

您可能想要:

  • 将该变量作为参数传递给 createPublishedPost 函数
  • ,或者将其声明为 global 位于函数内部(因此函数可以“看到”外部声明的变量)< /em>

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

作为旁注,您可能需要配置 error_reporting 级别(另请参阅),因此,当您使用未声明的变量时,您会得到一个 E_NOTICE ——在这种情况下,您应该得到一个;-)

您可能还想启用 display_errors,在您的开发计算机上,如果它尚未打开 - 似乎是,因为您收到了致命错误消息

一开始可能看起来有点烦人,但是,一旦你习惯了它,它真的很棒:可以更快地检测到这类东西;-)

它还有助于检测变量名称中的拼写错误^^

它让你的代码更加干净!

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.

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 :

  • 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 ;-)

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 !

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文