从Facebook(页面)JSON或Rss Feed获取图像链接

时间:2014-11-13 19:56:11

标签: php json facebook rss feed

我想在我的Facebook页面上发布所有帖子。 我知道我们可以为页面获取JSON或RSS格式的提要,但我在识别和解析帖子的图像链接方面存在问题。 例如,此处是指向我的页面Facebook Page JSON Feed的JSON供稿链接。

以下是您可以在那里看到的格式。

{
   "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": [

使用下面的方法我可以获得帖子的标题,但我不确定如何从每个帖子获取图像链接。有些人可以帮帮我吧。感谢

$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>";
}

我想从我的页面FEED中获取图像以显示在我的网站和Android应用程序上。感谢

2 个答案:

答案 0 :(得分:1)

使用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>';
}

在下面给出你的评论,你真的不需要做太多伪造,正如你在上面看到的那样。发送&#34; Mozilla&#34;因为用户代理足以通过他们的浏览器检查。但我认为你错过了这条线,它超越了Facebook的302重定向:

curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);

答案 1 :(得分:0)

快速而肮脏的解决方案: -

<?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'];
}