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

May 3, 2007 by User Imagelordtime
Filed under: Coding, Products, Various 

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;
}

Rate this:
2.5

Comments

One Comment on How to post remotely to blogger.com - Working PHP Example

  1. no imagelordtime (Who am I?) on Mon, 22nd Oct 2007 8:47 pm
  2. unfortunately i posted code with minor errors ( lines like
    }elseif( $arr[’subtitle’] ) {
    $entry .= ““;
    $entry .= ““;
    $entry .= ““;
    })

    this lines makes atom post body,

    assume its like

    $entry .= ““;
    $entry .= “{$arr[’subtitle’]}“;
    $entry .= “
    “;

    but later i’ll repost new code

    Rate this:
    2.5

Tell me what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!