2011-06-23 79 views
1

我試圖設置一個腳本,將刮我的Facebook頁面,並返回給我所有的信息,所以我可以將它插入數據庫(名稱,喜歡等..)。我建立了一個CURL腳本,但由於某種奇怪的原因而無法工作。它會拋出我「注意:嘗試在第26行的C:\ xampp \ XXX \ curltest.php中獲取非對象的屬性。CURL和JSON問題

是在我的服務器上啓用了JSON和CURL。如果有人會幫忙,我會很高興。 ;)

<?php 
// create both cURL resources 
$ch1 = curl_init(); 
$ch2 = curl_init(); 

// set URL and other appropriate options 
curl_setopt($ch1, CURLOPT_URL, "http://graph.facebook.com/19292868552"); 
curl_setopt($ch1, CURLOPT_HEADER, 0); 
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch2, CURLOPT_URL, "http://graph.facebook.com/youtube"); 
curl_setopt($ch2, CURLOPT_HEADER, 0); 
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1); 

//create the multiple cURL handle 
$mh = curl_multi_init(); 

//add the two handles 
curl_multi_add_handle($mh,$ch1); 
curl_multi_add_handle($mh,$ch2); 

$running=null; 
//execute the handles 
do { 
    usleep(10000); 
    $Likes = json_decode(curl_multi_exec($mh,$running)); 
    return $Likes->data; 

      //output the message body 
      echo($Likes->likes); 
      //add a line break to separate comments 
      echo("<br />"); 

} while ($running > 0); 



//close the handles 
curl_multi_remove_handle($mh, $ch1); 
curl_multi_remove_handle($mh, $ch2); 
curl_multi_close($mh); 


?> 

另外我想知道如何使某種「同時」功能。比方說,如果我想抓取10個URL,我無法一一寫入,所以我最好使用SQL查詢從那裏獲取這些URL。

在此先感謝。

回答

3

基本上,您的$Likes變量中沒有任何內容。您應該測試以確保您的執行和解碼成功。

該變量中沒有任何內容的原因是因爲json_decode()失敗。 json_decode()失敗的原因是curl_multi_exec()函數的輸出是cURL進程的句柄。如果你read the documentation,你會看到。

您需要使用curl_multi_getcontent()來獲取返回的數據。

+0

感謝好友 - curl_multi_getcontent()的偉大工程。 – Ricardo

0

如果您在看看這些網址都沒有指數稱爲data:這些是2 JSON響應:

{ 
    "id": "19292868552", 
    "name": "Facebook Platform", 
    "picture": "http://profile.ak.fbcdn.net/hprofile-ak-snc4/211033_19292868552_7506301_s.jpg", 
    "link": "http://www.facebook.com/platform", 
    "likes": 2158804, 
    "category": "Product/service", 
    "website": "http://developers.facebook.com", 
    "username": "platform", 
    "founded": "May 2007", 
    "company_overview": "Facebook Platform enables anyone to build social apps on Facebook and the web.", 
    "mission": "To make the web more open and social." 
} 

{ 
    "id": "7270241753", 
    "name": "YouTube", 
    "picture": "http://profile.ak.fbcdn.net/hprofile-ak-snc4/41601_7270241753_5799_s.jpg", 
    "link": "http://www.facebook.com/youtube", 
    "likes": 40013301, 
    "category": "Product/service", 
    "website": "http://www.facebook.com/youtube", 
    "username": "youtube", 
    "founded": "2005", 
    "company_overview": "YouTube is the the largest online video destination in the world and the third most visited Website overall. The site exceeds two billion views a day - nearly double the prime time audience of all three major U.S. networks combined. The platform comprises the largest video-sharing community in the world and includes users, advertisers and over 10,000 partners. Every minute 24 hours of video uploaded to the site. Hundreds of millions of users spanning the globe come to YouTube to discover and shape the world through video.", 
    "mission": "To be the best place on earth to create, watch, discover and share videos.", 
    "products": "More to explore at: http://www.youtube.com/testtube", 
    "description": "YouTube - We | Facebook" 
} 

還要確保json_decode工作(這也可能是一個問題)

0

因爲curl_multi_exec獲得非內容或者無法將內容轉換爲來自json的對象,所以您最好在調用$ Likes-> data之前嘗試if($ likes)。

你可以寫一個函數來處理一個單一的呼叫,但沒有使用curl_multi