Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. <?php
  2.  
  3. /*
  4. Posted at www.w3tools.info
  5. How to post to blogger using PHP with labels/categories/tags using gData api ?
  6. */
  7.  
  8. function createPublishedPost($title, $content,$tags){
  9.  
  10. /* DEFINE THESE */
  11.  
  12. $user = 'BLOGGER_USERNAME';
  13. $pass = 'BLOGGER_PASS';
  14. $blogID = 'YOUR_BLOG_ID';
  15.  
  16. $service = 'blogger';
  17.  
  18.     require_once'Zend/Loader.php';
  19.     Zend_Loader::loadClass('Zend_Gdata');
  20.     Zend_Loader::loadClass('Zend_Gdata_Query');
  21.     Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
  22.  
  23.     $client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service, null,
  24.               Zend_Gdata_ClientLogin::DEFAULT_SOURCE, null, null,
  25.               Zend_Gdata_ClientLogin::CLIENTLOGIN_URI, 'GOOGLE');
  26.     $gdClient = new Zend_Gdata($client);
  27.  
  28.   $uri = 'http://www.blogger.com/feeds/' . $blogID . '/posts/default';
  29.   $entry = $gdClient->newEntry();
  30.   $entry->title = $gdClient->newTitle($title);
  31.   $entry->content = $gdClient->newContent($content);
  32.   $entry->content->setType('html');
  33.   $tags = explode(",",$tags);
  34.   if(is_array($tags)){
  35.     $labels = array();
  36.     foreach($tags as $tag){
  37.         $labels[] = $gdClient->newCategory(trim($tag), 'http://www.blogger.com/atom/ns#');
  38.     }
  39. /* Adding tags to post */
  40.     $entry->setCategory($labels);
  41.  
  42.     }
  43.   $createdPost = $gdClient->insertEntry($entry, $uri);
  44.   $idText = split('-', $createdPost->id->text);
  45.   $newPostID = $idText[2];
  46.  
  47.   return $newPostID;
  48. }
  49.  
  50.  
  51.  
  52.  
  53.  
  54. /* USAGE OF THIS FUNCTION */
  55.  
  56. createPublishedPost("Sample post title","I'm an a post the demonstrates usage of gdata api to publish articles to a blogspot blog.","sampletag1,sampletag2");
  57.  
  58. // tags must be passed to function as comma separated values.
  59.  
  60. ?>