tags:

views:

153

answers:

4

I'm hacking together a simple php script that will build a list of photo albums I have on my Facebook fan page.

Facebook kindly offer the Graph API which gives me back a nice list of Albums, however they no longer provide the path of the default album image.

I want to write a PHP script that loads an album url via curl and somehow grab the path of the first image in the table that contains the thumbnails. This would be the "src" value of the first img tag that has a class of "UIPhotoGrid_Image".

The block of layout code that contains the good stuff looks like this:

<div id="album_container">
    <div class="UIPhotoGrid_Container UIPhotoGrid_DefaultPadding">
        <table class="UIPhotoGrid_Table" cellpadding="0" cellspacing="0">
            <tr>
                <td class="UIPhotoGrid_TableCell">
                    <a class="UIPhotoGrid_PhotoLink clearfix" href="http://www.facebook.com/photo.php?pid=5004658&amp;amp;id=20785087272"&gt;&lt;img class="UIPhotoGrid_Image img" src="http://photos-e.ak.fbcdn.net/hphotos-ak-snc4/hs080.snc4/35354_422883027272_20785087272_5004658_704231_s.jpg" onload="this.fb_loaded = true;" /></a>
                </td>
                <td class="UIPhotoGrid_TableCell">
                    <a class="UIPhotoGrid_PhotoLink clearfix" href="http://www.facebook.com/photo.php?pid=5004659&amp;amp;id=20785087272"&gt;&lt;img class="UIPhotoGrid_Image img" src="http://photos-c.ak.fbcdn.net/hphotos-ak-snc4/hs080.snc4/35354_422883032272_20785087272_5004659_6158094_s.jpg" onload="this.fb_loaded = true;" /></a>
                </td>
                <td class="UIPhotoGrid_TableCell">
                    <a class="UIPhotoGrid_PhotoLink clearfix" href="http://www.facebook.com/photo.php?pid=5004660&amp;amp;id=20785087272"&gt;&lt;img class="UIPhotoGrid_Image img" src="http://photos-f.ak.fbcdn.net/hphotos-ak-snc4/hs080.snc4/35354_422883037272_20785087272_5004660_1787119_s.jpg" onload="this.fb_loaded = true;" /></a>
                </td>
            </tr>
        </table>
    </div>
</div>

This sadly, is beyond my current coding capabilities... Any ideas?

A: 

You have several possibilities :

1) elegant one, you make a dom tree from your html sample and extract your tag

2) less elegant but efficient, you can use a regexp to extract that information

Guillaume Lebourgeois
A: 

You can pull the site source into a string with

   $url = “http://www.foo.com”;
   $str = file_get_contents($url);

Without a regular expression that will suit your needs, I can only say that in previous experiences I have used lots of conditional statements and relied heavily on substr(), however this is likely not wise.

Orbit
+1  A: 

You could use phpsimpledom to grab the path using a jQuery style syntax.

Note: Facebook probably have serveral image clusters, so the URL to the photo may change over time.

Tom
A: 

You should try YQL, its very easy and effective. Here is a great, short tutorial for it- http://motyar.blogspot.com/2010/01/parsing-html-with-yql-and-php-without.html

Motyar