0
votes

I am trying to get all the posts on my Facebook page. I know we can get the feeds in JSON or RSS format for a page but I am having issue in identifying and parsing the post's image Link. For example here is the link to the JSON feeds of my page Facebook Page JSON Feed.

Below is the format you can see there.

{
   "title": "Shorts Fashion's Facebook Wall",
   "link": "https:\/\/www.facebook.com\/",
   "self": "https:\/\/www.facebook.com\/feeds\/page.php?id=717795881626394&format=json",
   "updated": "2014-11-13T10:30:07-08:00",
   "icon": "http:\/\/www.facebook.com\/favicon.ico",
   "entries": [
      {
         "title": " Like <3",
         "id": "78d3340189c23524385e0522f6336f03",
         "alternate": "http:\/\/www.facebook.com\/717795881626394\/photos\/a.722551361150846.1073741829.717795881626394\/780062908733024\/?type=1",
         "categories": [

         ],
         "published": "2014-11-13T18:30:07+00:00",
         "updated": "2014-11-13T18:30:07+00:00",
         "author": {
            "name": "Shorts Fashion"
         },
         "verb": "",
         "target": "",
         "objects": "",
         "comments": "",
         "likes": "",
         "content": "Like \u2665\u003Cbr\/>\u003Cbr\/>\u003Ca href=\"\/717795881626394\/photos\/a.722551361150846.1073741829.717795881626394\/780062908733024\/?type=1&relevant_count=1\" id=\"\" title=\"\" target=\"\" onclick=\"\" style=\"\">\u003Cimg class=\"img\" src=\"https:\/\/scontent-b-kul.xx.fbcdn.net\/hphotos-xap1\/v\/t1.0-9\/s130x130\/10411933_780062908733024_2963383477612486789_n.jpg?oh=edcaf58eabb7f3fc88046bab3d5ddb5c&oe=54EA4DE2\" alt=\"\" \/>\u003C\/a>\u003Cbr\/>"
      },
      {
         "title": " <3 <3<3 <3<3 <3",
         "id": "adfedefb63af7a5443db66e19807f8fc",
         "alternate": "http:\/\/www.facebook.com\/717795881626394\/photos\/a.722551361150846.1073741829.717795881626394\/780058738733441\/?type=1",
         "categories": [

Using the method below I can get the Title of the post but I am not sure that how to get the Image link from each post. Can some please help me with it. Thanks

$url = "http://www.facebook.com/feeds/page.php?id=717795881626394&format=json";
// disguises the curl using fake headers and a fake user agent.
function disguise_curl($url)
{
$curl = curl_init();

$header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,";
$header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
$header[] = "Cache-Control: max-age=0";
$header[] = "Connection: keep-alive";
$header[] = "Keep-Alive: 300";
$header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
$header[] = "Accept-Language: en-us,en;q=0.5";
$header[] = "Pragma: "; // browsers keep this blank.

curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla');
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_REFERER, '');
curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate');
curl_setopt($curl, CURLOPT_AUTOREFERER, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_TIMEOUT, 10);

$html = curl_exec($curl); // execute the curl command
curl_close($curl); // close the connection

return $html; // and finally, return $html
}

// uses the function and displays the text off the website
$text = disguise_curl($url);

$json_feed_object = json_decode($text);


foreach ( $json_feed_object->entries as $entry )
{
echo " {$entry->title}";
echo "<br>";
}

I want to get images from my page FEED to display on my website and android app. Thanks

2

2 Answers

1
votes

Using DOM:

<?php

$url = "http://www.facebook.com/feeds/page.php?id=717795881626394&format=json";

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$json = curl_exec($curl);
curl_close($curl);

$json_feed_object = json_decode($json);

foreach ($json_feed_object->entries as $entry) {
    echo $entry->title . '<br>';
    $html = @DOMDocument::loadHTML($entry->content);
    $images = $html->getElementsByTagName('img');
    echo $images->item(0)->getAttribute('src') . '<br>';
}

Ref your comment below, you really don't need to do so much faking, as you can see above. Sending "Mozilla" as the user agent is enough to get past their browser check. But I think you were missing this line, which gets past Facebook's 302 redirects:

curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);

0
votes

A quick and dirty solution :-

<?php 

$url = "https://www.facebook.com/feeds/page.php?id=717795881626394&format=json";
// disguises the curl using fake headers and a fake user agent.

$curl = curl_init();
$header = array(
"host:www.facebook.com",
"path:/feeds/page.php?id=717795881626394&format=json",
"accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"accept-language:en-GB,en-US;q=0.8,en;q=0.6",
"cache-control:max-age=0",
"dnt:1",
"user-agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36");


curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_TIMEOUT, 10);

$html = curl_exec($curl); // execute the curl command
curl_close($curl); // close the connection


// uses the function and displays the text off the website
$json_feed = json_decode($html, true);
foreach($json_feed['entries'] as $entry)
{
echo $entry['title'] . "<br>";
preg_match("/<img class=\"img\" src=\"(.*)\" alt=\"\" \/>/", $entry['content'], $imgsrc);
echo $imgsrc['1'];
}