使用 Zend Gdata 发布到博客

发布于 2024-11-03 20:37:33 字数 1387 浏览 1 评论 0原文

我正在尝试使用以下代码发布到我的博客帐户,但它似乎不起作用。我是这方面的新手,我错过了什么吗?谢谢... `

<?php

$user = '[email protected]';
$pass = 'password';

// I have to admit, I would normally use the autoloader
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata');
Zend_Loader::loadClass('Zend_Gdata_Query');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
Zend_Loader::loadClass('Zend_Gdata_Feed');

$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, 'blogger', null,
    Zend_Gdata_ClientLogin::DEFAULT_SOURCE, null, null,
    Zend_Gdata_ClientLogin::CLIENTLOGIN_URI, 'GOOGLE');
$gdClient = new Zend_Gdata($client);

function createPublishedPost()
{
  $title='Hello, world!';
  $content='I am blogging on the internet.';
  $blogID= "4419618066922958909";
  $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; 
}

$ret = createPublishedPost();
echo $ret;
?>

`

I am trying to post to my blogger account using the below code but it does not seem to work. I am a newbie with this, am I missing anything? Thanks...
`

<?php

$user = '[email protected]';
$pass = 'password';

// I have to admit, I would normally use the autoloader
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata');
Zend_Loader::loadClass('Zend_Gdata_Query');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
Zend_Loader::loadClass('Zend_Gdata_Feed');

$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, 'blogger', null,
    Zend_Gdata_ClientLogin::DEFAULT_SOURCE, null, null,
    Zend_Gdata_ClientLogin::CLIENTLOGIN_URI, 'GOOGLE');
$gdClient = new Zend_Gdata($client);

function createPublishedPost()
{
  $title='Hello, world!';
  $content='I am blogging on the internet.';
  $blogID= "4419618066922958909";
  $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; 
}

$ret = createPublishedPost();
echo $ret;
?>

`

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

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

发布评论

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

评论(2

南冥有猫 2024-11-10 20:37:33

除了您使用的代码之外,我们很难知道出了什么问题。因此,首先您需要:

  • 检查身份验证是否引发任何异常
  • 检查 insertEntry 是否引发任何异常
  • 检查 insertEntry 返回的内容

请参阅代码中的注释以查看我更改的内容:

<?php

$user = '[email protected]';
$pass = 'password';

// I have to admit, I would normally use the autoloader
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata');
Zend_Loader::loadClass('Zend_Gdata_Query');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
Zend_Loader::loadClass('Zend_Gdata_Feed');

//Checks if getHttpClient throws any exceptions
try {
   $client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, 'blogger', null,
    Zend_Gdata_ClientLogin::DEFAULT_SOURCE, null, null,
    Zend_Gdata_ClientLogin::CLIENTLOGIN_URI, 'GOOGLE');
} catch (Zend_Gdata_App_AuthException $ae) {
   echo 'Problem authenticating: ' . $ae->exception() . "\n";
}

$gdClient = new Zend_Gdata($client);

function createPublishedPost()
{
  $title='Hello, world!';
  $content='I am blogging on the internet.';
  $blogID= "4419618066922958909";
  $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');

  //Checks if insertEntry throws any exceptions
  try{
      $createdPost = $gdClient->insertEntry($entry, $uri);
  } catch (Zend_Gdata_App_AuthException $ae) {
      echo 'Problem authenticating: ' . $ae->exception() . "\n";
  }

  var_dump($createdPost); //Will print the variable and what type it is
  $idText = split('-', $createdPost->id->text);
  $newPostID = $idText[2]; 

  return $newPostID; 
}

$ret = createPublishedPost();
echo $ret;
?>

It's hard for us to know what goes wrong without knowing anything more than what code you are using. So first of you need to:

  • Check if the authentication throws any exceptions
  • Check if the insertEntry throws any exceptions
  • Check what the insertEntry returns

See comments in code to see what I have changed:

<?php

$user = '[email protected]';
$pass = 'password';

// I have to admit, I would normally use the autoloader
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata');
Zend_Loader::loadClass('Zend_Gdata_Query');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
Zend_Loader::loadClass('Zend_Gdata_Feed');

//Checks if getHttpClient throws any exceptions
try {
   $client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, 'blogger', null,
    Zend_Gdata_ClientLogin::DEFAULT_SOURCE, null, null,
    Zend_Gdata_ClientLogin::CLIENTLOGIN_URI, 'GOOGLE');
} catch (Zend_Gdata_App_AuthException $ae) {
   echo 'Problem authenticating: ' . $ae->exception() . "\n";
}

$gdClient = new Zend_Gdata($client);

function createPublishedPost()
{
  $title='Hello, world!';
  $content='I am blogging on the internet.';
  $blogID= "4419618066922958909";
  $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');

  //Checks if insertEntry throws any exceptions
  try{
      $createdPost = $gdClient->insertEntry($entry, $uri);
  } catch (Zend_Gdata_App_AuthException $ae) {
      echo 'Problem authenticating: ' . $ae->exception() . "\n";
  }

  var_dump($createdPost); //Will print the variable and what type it is
  $idText = split('-', $createdPost->id->text);
  $newPostID = $idText[2]; 

  return $newPostID; 
}

$ret = createPublishedPost();
echo $ret;
?>
酒中人 2024-11-10 20:37:33

通过一些实验得到了它......在函数内移动了 $gdClient = new Zend_Gdata($client); 。以前它没有在全球范围内声明。

Got it with some experimentation...Moved $gdClient = new Zend_Gdata($client); inside the function. Previously it is not globally declared.

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