testtestHome

How to grab your Google Reader OPML using PHP and cURL

29.03.2009 by Mich - lire la version francophone - lees dit in het Nederlands

I don't like doing some things twice. When I subscribe to a blog, I want it to be added to my blogroll as well. Until now, I had to add the link manually to blog, since I don't want to use ugly widgets here. There is an easy solution, called OPML.

My idea was to fetch the OPML-file once a day from Google Reader and save it somewhere here. The blogroll will then be generated from the OPML each time some one will check my blogroll. Using this technique, there would only be a 24 hour delay in displaying new blogs I've subscribed to.

But there is an annoying part. To be able to download your OPML-file you have to sign on. That's pretty annoying. And for that I had to do something I never did before: use cURL with cookies.

It's actually quite simple: just fill in a Google sign on form from cURL and save what is returned for a cookie to a file. Use that file as cookie when accessing the OPML-file URL directly.

If you're using Google Reader, the URL of your OPML-file is the next :

http://www.google.com/reader/public/subscriptions/user/<user_id>/

You can obtain your user id by clicking on « All Items ». A URL will appear, containing your user id between two %2F's. In my case, it is ‘06937166373900766291’:

http://www.google.com/reader/view/#stream/user%2F06937166373900766291%2Fstate%2Fcom.google%2Freading-list

Here is the PHP-script that I used for signing on and grabbing the OPML-file. I used absolute paths, because I'm planning to use it in a crontab as well :

<?
$email="barney.stinson@gmail.com";
$password="br0";

$url = "https://www.google.com/accounts/ServiceLoginAuth?service=reader";
$params = "service=reader";
$params.= "&Email=".urlencode($email);
$params.= "&Passwd=$password";
$params.= "&continue=http://www.google.com/reader/";
$params.= "&hl=en";
$params.= "&nui=1";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_COOKIEJAR, '/tmp/cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, '/tmp/cookie.txt');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$data = curl_exec($ch);
curl_close($ch);

$ch2 = curl_init();
curl_setopt($ch2, CURLOPT_URL, "http://www.google.com/reader/public/subscriptions/user/06937166373900766291/");
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch2, CURLOPT_COOKIEJAR, '/tmp/cookie.txt');
curl_setopt($ch2, CURLOPT_COOKIEFILE, '/tmp/cookie.txt');
curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, false);
$data = curl_exec($ch2);
curl_close($ch2);

$fp = fopen("/home/emich.be/public_html/misc/google-reader-subscriptions.xml","w");
fputs($fp,$data);
fclose($fp);
unlink("/tmp/cookie.txt");
?>

Don't forget to replace login, password and user id in the above script. I've saved the cookie file in the /tmp folder because I don't want anyone to be able to access that file from the outside.

This script can also be written in bash script using cURL directly. But I've used the PHP-option because a lot of people just don't have command-line access to their web servers.

tags: . .

Add a comment - 1686 trackback(s)