2014-11-13 8 views
0

내 Facebook 페이지에서 모든 게시물을 가져 오려고합니다. 페이지의 JSON 또는 RSS 형식으로 피드를 가져올 수 있지만 게시물의 이미지 링크를 식별하고 구문 분석하는 데 문제가 있습니다. 예를 들어 여기 내 페이지 Facebook Page JSON Feed의 JSON 피드에 대한 링크가 있습니다.Facebook (Page) JSON 또는 Rss 피드에서 이미지 링크 받기

다음은 사용자가 볼 수있는 형식입니다.

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

. 감사합니다

답변

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

참조 아래에 코멘트, 당신은 정말, 너무 많은 날조 할 필요가 없습니다 위 참조. 사용자 에이전트로 "Mozilla"를 보내면 브라우저 검사를 통과 할 수 있습니다. 그러나 나는 당신이 페이스 북의 302 리디렉션이 바뀌지이 줄을 누락 한 생각 : 피드를 가져 오기 위해 추가

curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);

+0

전체 코드를 추가 할 수 있습니까? PHP를 사용하여 피드를 가져 오는 데 문제가 있습니까? 감사합니다 – farhangdon

+0

물론입니다. 당신은 거기에 당신이 정말로 필요 없어 많은 것을 가지고 ... – rjdown

+0

공지 사항 : 라인 15에서 비 개체의 속성을 얻으려고합니다. 이 문제가 발생했습니다 :/ – farhangdon

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

당신이 만족 또한 컬 부분 전체 코드를 추가 할 수 있습니까? 피드 가져 오기에 문제가 있습니다. – farhangdon

+0

좋아, 필자의 스크립트에 컬 부분을 추가했다. –