开发者

Publishing to blogger using Zend Gdata

开发者 https://www.devze.com 2023-03-01 03:34 出处:网络
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...

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 = 'username@gmail.com';
$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开发者_JS百科');

$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;
?>

`


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 = 'username@gmail.com';
$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;
?>


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号