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.
精彩评论