2011-04-24 85 views
2

* 更新 數據正在返回但未輸出到瀏覽器。當我在下面的答案中使用urlencode建議後,我查看了源代碼。**使用PHP提取FQL數據

用這個拉出我的頭髮。

基於以下鏈接:

facebook FQL overview

FQL Video table info

此代碼應努力拉我所需要的表中的數據:

ini_set("display_errors","2"); 
ERROR_REPORTING(E_ALL); 

$contents = file_get_contents('https://api.facebook.com/method/fql.query?query=SELECT vid, owner, title, description, thumbnail_link, embed_html, updated_time, created_time FROM video WHERE owner= *******myID****'); 

echo $contents; 

但我正在以下錯誤:

Warning: file_get_contents(https://api.facebook.com/method/fql.query?query=SELECT vid, owner, title, description, thumbnail_link, embed_html, updated_time, created_time FROM video WHERE owner= ******myID*****) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in /vservers/mywebsite/htdocs/test.php on line 5 

任何人都可以看到我做錯了什麼?

+0

如果您在嘗試此瀏覽器,你會得到結果嗎? – fingerman 2011-04-24 00:20:48

+0

以上錯誤是瀏覽器輸出的內容。 – Denoteone 2011-04-24 00:26:23

+0

我的意思是網址:https://api.facebook.com/method/fql.query?query=SELECT vid,擁有者,標題,描述,縮略圖鏈接,embed_html,updated_time,created_time從視頻WHERE所有者= ***** ** myID **** – fingerman 2011-04-24 00:32:58

回答

5

嘗試改變主線:

$enc = urlencode('SELECT vid, owner, title, description, thumbnail_link, embed_html, updated_time, created_time FROM video WHERE owner= *******myID****'); 
$contents = file_get_contents("https://api.facebook.com/method/fql.query?query=$enc"); 
+1

現在有一個不同的錯誤:警告:file_get_contents()[function.file-get-contents]:SSL:致命協議錯誤在第6行/vservers/lingua/htdocs/test.php – Denoteone 2011-04-24 00:15:45

1

您是否嘗試過Facebook的PHP SDK:

https://github.com/facebook/php-sdk/

$result = $facebook->api(array(
    'query' => 'SELECT whatever FROM wherever WHERE something = something', 
    'method' => 'fql.query')); 

哦,看看這個:

You could try adding "limit 25" to the end of the query, or if you need to get as many status updates as possible, you can go up to 500. If a query would return more than 500 records and there is no limit explicitly stated in the query, Facebook returns an empty result: no errors, no nothing.

這可能不是你的錯誤,但要知道

+0

我看了在這個文檔中(當然現在github不會出現)類需要一個應用程序密鑰和密鑰。這將涉及創建一個應用程序。我是否需要創建一個應用程序來提取我需要的數據? – Denoteone 2011-04-24 00:24:50

+0

處理我在那裏,我認爲它會工作,如果你把appid和appsecret設置爲NULL .... – fingerman 2011-04-24 00:32:31

1

此錯誤是因爲空間在URL請求而造成這一點很重要:

query=SELECT vid, owner 
------------^----^----- 

空間更改爲%20 - 有用。 最簡單的方法就是str_replace整個URL,或進行urlencode查詢只(James C建議,上述),然後,如果需要的話,stripslashes你得到$內容

ini_set("display_errors","2"); 
ERROR_REPORTING(E_ALL); 

$url = 'https://api.facebook.com/method/fql.query?query=SELECT vid, owner, title, description, thumbnail_link, embed_html, updated_time, created_time FROM video WHERE owner= *******myID****'; 
$contents = file_get_contents(str_replace(" ", "%20", $url)); 
$contents = stripslashes($contents); 

echo $contents; 

// FETCHING RESULTS IT IN TO ARRAY: 
$array = json_decode($contents, true); 
print_r($array);