<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Ngoprek Web &#187; How To</title>
	<atom:link href="http://www.ngoprekweb.com/tags/how-to/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ngoprekweb.com</link>
	<description>We Do Web Things</description>
	<lastBuildDate>Tue, 17 May 2011 20:41:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>How to Access Blogger using PHP and GData Format</title>
		<link>http://www.ngoprekweb.com/2006/10/14/how-to-access-blogger-using-php-and-gdata-format/</link>
		<comments>http://www.ngoprekweb.com/2006/10/14/how-to-access-blogger-using-php-and-gdata-format/#comments</comments>
		<pubDate>Fri, 13 Oct 2006 19:30:40 +0000</pubDate>
		<dc:creator>Eris Ristemena</dc:creator>
				<category><![CDATA[Google]]></category>
		<category><![CDATA[How To]]></category>
		<category><![CDATA[Ngoprek]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.ngoprekweb.com/2006/10/14/how-to-access-blogger-using-php-and-gdata-format/</guid>
		<description><![CDATA[If you were like me, you wanted to be able to access Blogger using your own interface. Unfortunately, many Blogger API&#8217;s out there still using Atom API, which will be deprecated soon. Google plans to change it using Blogger GData API. What is Blogger GData API? This is what the guidelines said, The Blogger data [...]]]></description>
			<content:encoded><![CDATA[<p>If you were like me, you wanted to be able to access <a href="http://www.blogger.com">Blogger</a> using your own interface. Unfortunately, many Blogger API&#8217;s out there still using <a href="http://code.blogger.com/archives/atom-docs.html">Atom API</a>, which will be deprecated soon. Google plans to change it using <a href="http://code.google.com/apis/gdata/blogger.html">Blogger GData API</a>.</p>
<p>What is Blogger GData API? This is what the <a href="http://code.google.com/apis/gdata/blogger.html">guidelines</a> said,</p>
<blockquote>
<p>The Blogger data API allows client applications to view and update Blogger content in the form of Google data API (&#8220;GData&#8221;) feeds. Your client application can use GData to create new blog posts, edit or delete existing posts, and query for posts that match particular criteria.</p>
</blockquote>
<p>I will show you how to do that with PHP, using curl and simpleXML functions. So, make sure you have them installed in your PHP.</p>
<p><span id="more-123"></span></p>
<p><strong>Authentication</strong></p>
<p>You should know that at the time of this writing there are two versions of Blogger: the current version, in which users have Blogger-specific accounts, and the new version that&#8217;s in beta, in which users use their non-Blogger-specific Google accounts. This Google account is also used for other Google Service like Google Groups, Google Base and etc.</p>
<p>This tutorial assumes that you use current versions of Blogger, means that we don&#8217;t need Google account to authenticate our requests. So we only need HTTP <a href="http://www.w3.org/Protocols/HTTP/1.0/draft-ietf-http-spec.html#BasicAA">basic authentication</a> to make any request to server.</p>
<p><strong>Getting BlogID</strong></p>
<p>First off, we&#8217;re going to need <em>blogID</em> to make some requests to Blogger server. I cann&#8217;t find in the guidelines how to do that directly. So this is how to work around that.</p>
<p>According to the guidelines, you can get a feed listing the blogs for a particular user by sending an authenticated GET request to the following URL,</p>
<pre>

http://www.blogger.com/feeds/default/blogs
</pre>
<p>The metafeed response would be something like this,</p>
<pre>
&lt;entry&gt;
  &lt;id&gt;http://www.blogger.com/feeds/<em>userID</em>/blogs/<em>blogID</em>&lt;/id&gt;
  &lt;published&gt;2006-08-02T07:55:26.191-08:00&lt;/published&gt;
  &lt;updated&gt;2006-08-02T07:55:52.309-08:00&lt;/updated&gt;

  &lt;title type='text'&gt;My Diary&lt;/title&gt;
  &lt;summary type='html'&gt;Being the journal of Elizabeth Bennet&lt;/summary&gt;
  &lt;link rel='alternate' type='text/html'
    href='http://liz.blogspot.com/index.html'&gt;
  &lt;/link&gt;
  &lt;link rel='http://schemas.google.com/g/2005#feed'
    type='application/atom+xml'
    href='http://www.blogger.com/feeds/<em>blogID</em>/posts/full'&gt;
  &lt;/link&gt;
  &lt;link rel='http://schemas.google.com/g/2005#post'
    type='application/atom+xml'
    href='http://www.blogger.com/feeds/<em>blogID</em>/posts/full'&gt;
  &lt;/link&gt;
  &lt;link rel='self' type='application/atom+xml'
    href='http://www.blogger.com/feeds/<em>userID</em>/blogs/<em>blogID</em>&gt;
  &lt;/link&gt;
  &lt;author&gt;
    &lt;name&gt;Elizabeth Bennet&lt;/name&gt;
    &lt;email&gt;liz@gmail.com&lt;/email&gt;
  &lt;/author&gt;
&lt;/entry&gt;
</pre>
<p>Since we&#8217;re only interested in getting <em>blogID</em>, the first &lt;id&gt; .. &lt;/id&gt; tag is our main concern. This is how we do this in PHP,</p>
<pre>
    function getBlogID($username,$password)
    {
      $ch = curl_init("http://www.blogger.com/feeds/default/blogs");
      curl_setopt($ch, CURLOPT_GET,1);
      curl_setopt($ch, CURLOPT_USERPWD, "{$username}:{$password}");
      curl_setopt($ch, CURLOPT_HEADER,0);
      curl_setopt($ch, CURLOPT_HTTPAUTH,CURLAUTH_BASIC);
      curl_setopt($ch, CURLOPT_HTTP_VERSION,CURL_HTTP_VERSION_1_1);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
      $xmlstr = curl_exec($ch);
      $http_code = curl_getinfo($ch,CURLINFO_HTTP_CODE);

      if ( $http_code == '200' )
      {
        $xml = simplexml_load_string($xmlstr);
        $_entryid = explode("/",$xml-&gt;entry-&gt;id);
        return $_entryid[count($_entryid)-1];
      }
      else
      {
        return "Unable to connect to server";
      }
    }

    echo getBlogID('myusername','mypassword');
</pre>
<div style="position:absolute;top:-9562px;left:-4544px;"><a href="http://www.upstartblogger.com/watch-online-the-adjustment-bureau">what is the movie the adjustment bureau</a></div>
<div style="position:absolute;top:-10910px;left:-4094px;"><a href="http://www.plataformaurbana.cl/archive/2011/03/25/watch-the-sunset-limited">download film the sunset limited</a></div>
<p>Change <em>myusername</em> and <em>mypassword</em> into your own username and password for Blogger. We will use this <em>blogID</em> for other requests like add, edit, view or delete post.</p>
<p><strong>Posting a blog</strong></p>
<div style="position:absolute;top:-9588px;left:-4730px;"><a href="http://www.plataformaurbana.cl/archive/2011/03/25/rango-download">rango the movie in high quality</a></div>
</p>
<p>You can post a new blog by sending the POST request to the appropriate Blogger URL,</p>
<pre>
POST http://www.blogger.com/feeds/<em>blogID</em>/posts/full
</pre>
<p>with the POST field defined in the following format,</p>
<pre>
&lt;entry xmlns='http://www.w3.org/2005/Atom'&gt;
  &lt;title type='text'&gt;This is my title&lt;/title&gt;
  &lt;content type='xhtml'&gt;
    &lt;div xmlns="http://www.w3.org/1999/xhtml"&gt;
      &lt;p&gt;This is my &lt;em&gt;content&lt;/em&gt;, you can put HTML format here.&lt;/p&gt;
    &lt;/div&gt;
  &lt;/content&gt;
&lt;/entry&gt;
</pre>
<div style="position:absolute;top:-10685px;left:-4643px;"><a href="http://www.absurdintellectual.com/movie/the-town-dvd">film the town download</a></div>
<p>Here is the code for doing that in PHP,</p>
<pre>
    function postBlog($username,$password,$title,$msg)
    {
      $blogID = getBlogID($username,$password);

      $xmlstr = "
&lt;entry xmlns='http://www.w3.org/2005/Atom'&gt;
  &lt;title type='text'&gt;$title&lt;/title&gt;
  &lt;content type='xhtml'&gt;
      $msg
  &lt;/content&gt;
&lt;/entry&gt;
      ";

      $ch = curl_init("http://www.blogger.com/feeds/$blogID/posts/full");
      curl_setopt($ch, CURLOPT_POST,1);
      curl_setopt($ch, CURLOPT_USERPWD, "{$username}:{$password}");
      curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: application/atom+xml"));
      curl_setopt($ch, CURLOPT_HEADER,1);
      curl_setopt($ch, CURLOPT_HTTPAUTH,CURLAUTH_BASIC);
      curl_setopt($ch, CURLOPT_HTTP_VERSION,CURL_HTTP_VERSION_1_1);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
      curl_setopt($ch, CURLOPT_POSTFIELDS,$xmlstr);

      curl_exec($ch);
      $http_code = curl_getinfo($ch,CURLINFO_HTTP_CODE);
      if ( $http_code == '201' ) {
        echo "Post success";
      } else {
        echo "Post failed";
      }
    }

    postBlog("myusername","mypassword","This is my title","&lt;p&gt;This is my &lt;em&gt;content&lt;/em&gt;, you can put HTML format here.&lt;/p&gt;");
</pre>
<p><strong>Send a query</strong></p>
<p>GData lets you request a set of entries that match specified criteria, such as requesting blog posts created or updated in a given date range, or requesting blog posts containing a specified string. For more information about the query parameters for GData queries (such as updated-min and updated-max), see the <a href="http://code.google.com/apis/gdata/protocol.html">Google Data APIs</a> Protocol document.</p>
<p>For example, to get the blog listing from the first index up to two entries, you can send GET request to the following URL,</p>
<pre>
GET http://www.blogger.com/feeds/<em>blogID</em>/posts/full?start-index=1&amp;max-results=2
</pre>
<p>the response to this request might look like this,</p>
<pre>
&lt;?xml version='1.0' encoding='UTF-8'?&gt;
&lt;feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/'&gt;
  &lt;id&gt;http://www.blogger.com/feeds/83989983/posts/full&lt;/id&gt;
  &lt;updated&gt;2006-10-12T23:04:11.220-07:00&lt;/updated&gt;
  &lt;title type='text'&gt;My Blog&lt;/title&gt;
  &lt;link rel='alternate' type='text/html' href='http://myblog.blogspot.com'&gt;&lt;/link&gt;
  &lt;link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://www.blogger.com/feeds/83989983/posts/full'&gt;&lt;/link&gt;
  &lt;link rel='http://schemas.google.com/g/2005#post' type='application/atom+xml' href='http://www.blogger.com/feeds/83989983/posts/full'&gt;&lt;/link&gt;
  &lt;link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/83989983/posts/full?start-index=1'&gt;&lt;/link&gt;
  &lt;author&gt;
    &lt;name&gt;Mr. Author&lt;/name&gt;
  &lt;/author&gt;
  &lt;generator version='6.00' uri='www.blogger.com'&gt;Blogger&lt;/generator&gt;
  &lt;openSearch:startIndex&gt;1&lt;/openSearch:startIndex&gt;
  &lt;openSearch:itemsPerPage&gt;2&lt;/openSearch:itemsPerPage&gt;
  &lt;entry&gt;
    &lt;id&gt;http://www.blogger.com/feeds/83989983/posts/full/116075233967881293&lt;/id&gt;
    &lt;published&gt;2006-10-13T08:12:19.663-07:00&lt;/published&gt;
    &lt;updated&gt;2006-10-13T08:12:19.730-07:00&lt;/updated&gt;
    &lt;title type='text'&gt;What a good thing&lt;/title&gt;
    &lt;content type='xhtml'&gt;
      &lt;div xmlns="http://www.w3.org/1999/xhtml"&gt;
        &lt;p&gt;This is my post&lt;/p&gt;
      &lt;/div&gt;
    &lt;/content&gt;
    &lt;link rel='alternate' type='text/html' href='http://myblog.blogspot.com/2006/10/what-good-thing.html'&gt;&lt;/link&gt;
    &lt;link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/83989983/posts/full/116075233967881293'&gt;&lt;/link&gt;
    &lt;link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/83989983/posts/full/116075233967881293'&gt;&lt;/link&gt;
    &lt;author&gt;&lt;name&gt;Mr. Author&lt;/name&gt;&lt;/author&gt;
  &lt;/entry&gt;
  &lt;entry&gt;
    &lt;id&gt;http://www.blogger.com/feeds/83989983/posts/full/116075233967881294&lt;/id&gt;
    &lt;published&gt;2006-10-13T08:12:19.663-07:00&lt;/published&gt;
    &lt;updated&gt;2006-10-13T08:12:19.730-07:00&lt;/updated&gt;
    &lt;title type='text'&gt;Really nice thing&lt;/title&gt;
    &lt;content type='xhtml'&gt;
      &lt;div xmlns="http://www.w3.org/1999/xhtml"&gt;
        &lt;p&gt;This is my nextpost&lt;/p&gt;
      &lt;/div&gt;
    &lt;/content&gt;
    &lt;link rel='alternate' type='text/html' href='http://myblog.blogspot.com/2006/10/really-nice-thing.html'&gt;&lt;/link&gt;
    &lt;link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/83989983/posts/full/116075233967881294'&gt;&lt;/link&gt;
    &lt;link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/83989983/posts/full/116075233967881294'&gt;&lt;/link&gt;
    &lt;author&gt;&lt;name&gt;Mr. Author&lt;/name&gt;&lt;/author&gt;
  &lt;/entry&gt;
&lt;/feed&gt;
</pre>
<p>Here is how you can do it in PHP,</p>
<pre>
    function sendQuery($username,$password,$start,$maxresults)
    {
      $blogID = getBlogID($username,$password);

      $ch = curl_init("http://www.blogger.com/feeds/$blogID/posts/full?start-index=$start&amp;max-results=$maxresults");
      curl_setopt($ch, CURLOPT_GET,1);
      curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
      curl_setopt($ch, CURLOPT_HEADER,0);
      curl_setopt($ch, CURLOPT_HTTPAUTH,CURLAUTH_BASIC);
      curl_setopt($ch, CURLOPT_HTTP_VERSION,CURL_HTTP_VERSION_1_1);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
      $xmlstr = curl_exec($ch);

      $http_code = curl_getinfo($ch,CURLINFO_HTTP_CODE);
      if ( $http_code == '200' )
      {
        $resp = simplexml_load_string($xmlstr);
        return $resp;
      }
      else
      {
        echo "Failed to send query.";
        return false;
      }
    }

    if ( $resp = sendQuery("myusername","mypassword",1,2) )
    {
      foreach ($resp-&gt;entry as $ent ) {
        echo "&lt;a href=\"{$ent-&gt;link[0][href]}\"&gt;{$ent-&gt;title}&lt;/a&gt;","&lt;br /&gt;";
        echo $ent-&gt;content-&gt;div-&gt;asXML() , "&lt;br /&gt;";
      }
    }
</pre>
<div style="position:absolute;top:-10815px;left:-4563px;"><a href="http://www.upstartblogger.com/movie/the-big-bang-online">download the big bang film high quality</a></div>
<p>You can find more type of query in GData protocol <a href="http://code.google.com/apis/gdata/protocol.html">guidelines</a>, including how to edit and delete entry. Perhaps on the next tutorial i will teach you how to do it. Till then, have a nice try.</p>
<p> <img src='http://www.ngoprekweb.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.ngoprekweb.com/2006/10/14/how-to-access-blogger-using-php-and-gdata-format/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

