How to post remotely to blogger.com – Working PHP Example

Recently i met curious problem ? how to make remote post to blogger.com.

This task was really trivial before Google implemented their complex authentication mechanisms. After spending 3 hours to debug things i made simple solution which is working fine for me.

All code was written and based on GData API .

I will not comment my code below ? you must easily inderstand it yourself:

  • $arr ? array to post ( indexes ? title, subtitle, body, d_date, etc)
  • $blog ? external blog parameters. $blog['xmlrpc'] ? XML RPC URL of your blogger blog to post to (see link above)

Here we go:

$entry .= "\r\n";
$entry .= "";
$entry .= "
" . date("c", strtotime($arr['d_date'])) . "";
<![CDATA[{$arr['title']}]]> $entry .= "";

if( $arr['category'] ) {
$entry .= "";
}

if( $arr['body'] ) {
$entry .= "";
$entry .= "";
$entry .= "";
if( $arr['subtitle'] ) {
$entry .= "";
$entry .= "";
$entry .= "";
}
}elseif( $arr['subtitle'] ) {
$entry .= "";
$entry .= "";
$entry .= "";
}
if( $arr['author'] ) {
$entry .= "";
$entry .= "{$arr['author']}";
$entry .= "";
}
$entry .= "";

if( ! $this->blog_token[$this->blog_id]['Auth'] )
{
// AUTHENTICATE WITH GOOGLE !!!
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.google.com/accounts/ClientLogin" );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 4);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "Email={$blog['auth_login']}&Passwd={$blog['auth_pwd']}&service=blogger&source=Nst-Nst-4.0");
curl_setopt($ch, CURLOPT_POST, true);

$this->client->auth = curl_exec($ch);

if (curl_errno($ch)) {
$this->ret = curl_error($ch);
break;
} else {
curl_close($ch);
$this->ret = 0;

if( strpos( $this->client->auth, 'Auth=') == false) {
$this->ret = 1;
$this->clent->error = "Google Unauthorized";
break;
}
preg_match_all('!(\S+)=(\S+)!msi', $this->client->auth, $o, PREG_SET_ORDER);
for($_i=0; $_i
$this->blog_token[$this->blog_id][$o[$_i][1]] = $o[$_i][2];
}
}
}

$ch = curl_init();

$headers = array( "Authorization: GoogleLogin Auth={$this->blog_token[$this->blog_id]['Auth']}", "Content-type: application/atom+xml" );
curl_setopt($ch, CURLOPT_URL, $blog['xmlrpc']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 4);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_UNRESTRICTED_AUTH, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $entry);
curl_setopt($ch, CURLOPT_POST, true);

$this->client->message = curl_exec($ch);

if (curl_errno($ch)) {
$this->ret = curl_error($ch);
} else {
curl_close($ch);
$this->ret = 0;
}

One thought on “How to post remotely to blogger.com – Working PHP Example

  1. Carlo

    Hi... in this code there is a "for" argument statement opened and never closed. Is it incomplete?
    Many thanks.

Comments are closed.