12.23.2011

Posting to Blogger using PHP Zend Framework

Posting to Blogger using PHP Zend Framework

Here is a simple code for posting blog to blogger via PHP Zend Framework

  1. <?php
  2. require_once 'Zend/Loader.php';
  3. Zend_Loader::loadClass('Zend_Gdata');
  4. Zend_Loader::loadClass('Zend_Gdata_Query');
  5. Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
  6. $user = 'name@example.com';
  7. $pass = 'password';
  8. $service = 'blogger';
  9. $client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service, null,
  10.         Zend_Gdata_ClientLogin::DEFAULT_SOURCE, null, null,
  11.         Zend_Gdata_ClientLogin::CLIENTLOGIN_URI, 'GOOGLE');
  12.        
  13. $gdClient = new Zend_Gdata($client);
  14. $blogID = 'your blog id'; // like 98744313216546562
  15. $title='Hello, world!',
  16. $content='I am blogging on the internet.'
  17. $uri = 'http://www.blogger.com/feeds/' . $blogID . '/posts/default';
  18. $entry = $gdClient->newEntry();
  19. $entry->title = $gdClient->newTitle($title);
  20. $entry->content = $gdClient->newContent($content);
  21. $entry->content->setType('text');
  22. $createdPost = $gdClient->insertEntry($entry, $uri);
  23. $idText = split('-', $createdPost->id->text);
  24. $newPostID = $idText[2];
  25. echo $newPostID;
  26. ?>

Publishing a blog post using Blogger Data API

Publishing a blog post using Blogger Data API

  1. <?php
  2. $email = "username@gmail.com";
  3. $pass = "password for blogspot you use";
  4. $blogID= urlencode("your blog id"); // like 6304924319904337556
  5. // Do Not Modify Below Code
  6. if(!isset($_SESSION['sessionToken'])) {
  7. $ch = curl_init("https://www.google.com/accounts/ClientLogin?Email=$email&Passwd=$pass&service=blogger&accountType=GOOGLE");
  8. curl_setopt($ch, CURLOPT_POST,1);
  9. curl_setopt($ch, CURLOPT_FOLLOWLOCATION ,1);
  10. curl_setopt($ch, CURLOPT_HEADER,0);
  11. curl_setopt($ch, CURLOPT_RETURNTRANSFER ,1);
  12. //curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  13. $result = curl_exec($ch);
  14. $resultArray = curl_getinfo($ch);
  15. curl_close($ch);
  16. $arr = explode("=",$result);
  17. $token = $arr[3];
  18. $_SESSION['sessionToken'] = $token;
  19. }
  20. $entry = "<entry xmlns='http://www.w3.org/2008/Atom'><title type='text'>My </title><content type='xhtml'> This is my testing contnet </content></entry>";
  21. $len = strlen($entry);
  22. $headers = array("Content-type: application/atom+xml","Content-Length: {$len}","Authorization: GoogleLogin auth={$_SESSION['sessionToken']}","$entry");
  23. $ch = curl_init();
  24. curl_setopt($ch, CURLOPT_URL, "https://www.blogger.com/feeds/$blogID/posts/default");
  25. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  26. curl_setopt($ch, CURLOPT_TIMEOUT, 4);
  27. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  28. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  29. curl_setopt($ch, CURLOPT_UNRESTRICTED_AUTH, 1);
  30. curl_setopt($ch, CURLOPT_POST, true);
  31. //curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  32. $result = curl_exec($ch);
  33. $ERROR_CODE = curl_getinfo($ch);
  34. curl_close($ch);
  35. echo '<pre>';
  36. print_r($headers);
  37. var_dump($result);
  38. print_r($ERROR_CODE);
  39. ?>