Skip to content

Instantly share code, notes, and snippets.

@arbo77
Created July 29, 2012 20:21
Show Gist options
  • Save arbo77/3201644 to your computer and use it in GitHub Desktop.
Save arbo77/3201644 to your computer and use it in GitHub Desktop.
Blogspot content parser using PHP CUrl, last update using Google Blogger API
<?php
/*
* read more on :
* https://developers.google.com/blogger/docs/3.0/getting_started
*
*/
$key["server"] = "YOUR_BLOGGER_API_KEY";
$blogId = "YOUR_BLOG_API";
$url = "https://www.googleapis.com/blogger/v3/blogs/$blogId/posts?key=$key[server]";
try{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPGET, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$resp = curl_exec($ch);
curl_close($ch);
$posts = json_decode($resp, true);
// do whatever you want with this data responses
}catch(Exception $ex){
echo $ex->getMessage();
}
?>
<?php
/*
* read more on :
* https://developers.google.com/blogger/docs/3.0/getting_started
*
*/
$key["server"] = "YOUR_BLOGGER_API_KEY";
$blogId = "YOUR_BLOG_API";
$url = "https://www.googleapis.com/blogger/v3/blogs/$blogId/posts/$_GET[id]?key=$key[server]";
try{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPGET, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$resp = curl_exec($ch);
curl_close($ch);
$posts = json_decode($resp, true);
// do whatever you want with this data responses
}catch(Exception $ex){
echo $ex->getMessage();
}
?>
<?php
function parseBlogSpot($url){ // $url is full blogspot URL
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPGET, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "Opera/9.50 (J2ME/MIDP; Opera Mini/4.0.9800/209; U; en)");
$resp = curl_exec($ch);
curl_close($ch);
$lines = explode("\n",$resp);
$start = "<div class='post hentry'>"; // start marker
$end = "<div class='post-footer'>"; // stop marker
$tmp = "";
$doParse = false;
foreach($lines as $line){
if(trim($line) == $start){
$doParse = true;
}
if(trim($line) == $end){
break;
}
if($doParse){
$tmp .= $line."\n";
}
}
return $tmp;
}
?>
@xeoncross
Copy link

I recommend you learn about PHP's DOM parsing and things like SimpleXML. Much more powerful and robust than looking for starting and ending strings.

@arbo77
Copy link
Author

arbo77 commented Jul 30, 2012

Hi, thank you for your recommendation. I know DOM or SimpleXML are powerful, but did you ever try parse blogspot content using DOM or SimpleXML without error? I've done that, and always got an error when parsing, the problem is DOM or SimpleXML cannot parse content with embedded script.

@xeoncross
Copy link

Well, actually there is a simple solution to libxml components.

libxml_use_internal_errors(true);

@indiarocks08
Copy link

how to create a new post in blogger using api v3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment