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 ?
You can use the PHP client library to publish new blog entries.
You can use the Python client library to publish new blog entries. First, create a GDataEntry instance to represent the blog post. Then you can set the title, content and other attributes of the blog post. Finally, use the GDataService instance to insert the post.
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 :
createPublishedPost
functionglobal
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 !
Moving the $gdClient
to the function body fixed something, but you also have to move the $blogID
into the function body.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With