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: . .

1 comment(s)

Extracting GPS data from a photo's EXIF-data using PHP…

01.02.2009 by Mich - lire la version francophone (2) - lees dit in het Nederlands

I recently bought a new mobile phone, the Nokia N79, successor of my old N73. The N79 has a GPS-chip and a camera, which means geotagging fun !

The N79 stores GPS-data in the EXIF-information of the photo. By default, this is disabled. You'll have to turn it on in the settings of the camera:

Once activated, GPS-coordinates will be stored in the EXIF-headers. You can read those using the following code:

Reading EXIF data:
$exif = exif_read_data('dummyimage.jpg',0,TRUE);

Degrees:
$deg=$exif["GPS"]["GPSLatitude"][0];

Minutes:
$min=$exif["GPS"]["GPSLatitude"][1];

Seconds:
$sec=$exif["GPS"]["GPSLatitude"][2];

Hemisphere (N, S, W ou E):
$hem=$exif["GPS"]["GPSLatitudeRef"];

Altitude:
$alt=$exif["GPS"]["GPSAltitude"][0];

(for longitude, replace "GPSLatitude" by "GPSLongitude" in the above code)

I'm using this code to grab GPS data out of pictures so I can geotag blog-entries posted from my mobile.

tags: . . .

Add a comment

How to link Google Earth to your photoblog…

11.03.2006 by Mich

Haven’t you ever wanted to map all your pictures on the earth surface? There already are some things like Mappr, but it’s only available in the US and is only for those with Flickr accounts. I myself programmed my own photoblog, which makes it more difficult to use a tool like that, especially when I live in Europe.

While I was saving some points of interest in Google Earth, I noticed they had 2 formats: “.kmz” and “.kml”. Since I was curious, I saved the file in both formats and opened them in an editor. The “.kmz” was just a boring binary file where I could not understand a clue of the special characters, while the “.kml” was an xml-file. I googled for some more information and landed on a tutorial page as well as a general reference.

Since I already linked all my pictures with Google Maps by keeping their longitude and latitude, I was only one step away to generate a file that would point out all my pictures in Google Earth. So there I went: I fetched all the rows out of my database that contained longitude and latitude and iterated over the list to generate all the “Placemark” items.

Basically, this is how my PHP-script generates the “<placemark>” items:

<?
        
echo "<Placemark>";
        echo
"<description><![CDATA[<a href="http://visual.emich.be/?picid=$id">$name<br><img src="http://www.emich.be/images/$dir/tmb$file" alt="$name"/></a><br>$description]]></description>";
        echo
"<name>$name</name>";
        echo
"<LookAt><longitude>$ycoord</longitude>";
        echo
"<latitude>$xcoord</latitude>";
        echo
"<range>540.68</range>";
        echo
"<tilt>0</tilt>";
        echo
"<heading>3</heading>";
        echo
"</LookAt>";
        echo
"<Point>";
        echo
"<coordinates>$ycoord,$xcoord,0</coordinates>";
        echo
"</Point>";
        echo
"<Style><IconStyle><Icon><href>root://icons/palette-4.png</href>
        <x>192</x><y>64</y>
        <w>32</w><h>32</h></Icon></IconStyle></Style>"
;
        echo
"</Placemark>";
?>

The result is different placemarks, like this one:

<Placemark>
    <description>
        <![CDATA[ <a href="http://visual.emich.be/?picid=2">Le bout du tunnel<br><img src="http://www.emich.be/images/misc/tmbbrol140505.JPG" alt="Le bout du tunnel"/></a><br>Et tout en haut, il y a Maria
    ]]>
    </description>
    <name>Le bout du tunnel</name>
    <LookAt>
        <longitude>4.36772222222</longitude>
        <latitude>50.8488722222</latitude>
        <range>540.68</range>
        <tilt>0</tilt>
        <heading>3</heading>
    </LookAt>
    <Point>
        <coordinates>4.36772222222,50.8488722222,0</coordinates>
    </Point>
    <Style>
        <IconStyle>
            <Icon>
                <href>root://icons/palette-4.png</href>
                <x>192</x>
                <y>64</y>
                <w>32</w>
                <h>32</h>
            </Icon>
        </IconStyle>
    </Style>
</Placemark>

Finally, I just have to set the central point of my “globe” in the root "<Document>"-tag. In my case, this would be Brussels, Belgium.

<LookAt>
  <longitude>4.351995</longitude>   <latitude>50.846617</latitude>
  <range>5000</range>
  <tilt>0</tilt>
  <heading>3</heading>
</LookAt>

The result is a .kml-file that can be downloaded at this location and will open in Google Earth, so you can see all the spots of which I have a picture. It displays a little camera at each spot, and the description has a thumbnail. If you click on the thumbnail, you'll arrive on the pages on the photoblog.

I am thinking of writing a script for flickr, that works the same way as mappr, but for Google Earth. Using 2 tags and a specific tag and for latitude/longitude. Maybe even more…

The advantage of generating a KML-file is you don't have to do any maintenance on it!

Anyway, I just wanted to share this with others.

tags: . . . .

1 comment(s)