2013-04-20 54 views
0

我是新來的stackoverflow和編程。我只是在學習php,並決定使用facebook place search api來進行練習。如何接收facebook圖表api返回的數據?

我已經寫了下面的代碼在PHP:

$FacebookGraphURL = 'https://graph.facebook.com/search?q='.$keyword.'&type=place' . htmlentities('&center=') .$center.'&distance='.$distance.'&access_token='.ACCESS_TOKEN; 

$FacebookGraphJSON = @file_get_contents($FacebookGraphURL); 
$dataArray = json_decode($FacebookGraphJSON); 

但它不返回任何東西。我認爲這不是從圖api得到的適當方式。我搜索了很多,但找不到一個我可以理解的我作爲新手。

任何人都可以幫助我。

回答

0

1)htmlentities('&center=') .$center&center=' . htmlentities($center) .

2)不要使用@file_get_contents在此之前,當您的文件請求失敗會抑制錯誤(在這種情況下的警告)。

試試下面的代碼:

error_reporting(E_ALL); 
ini_set('display_errors', 'On'); 
define('ACCESS_TOKEN','**********************************************'); 
$keyword = 'coffee'; 
$center= urlencode('37.76,-122.427'); 
$distance=1000; 
$FacebookGraphURL = 'https://graph.facebook.com/search?q='.$keyword.'&type=place&center='.$center.'&distance='.$distance.'&access_token='.ACCESS_TOKEN; 
$FacebookGraphJSON = file_get_contents($FacebookGraphURL); 
$dataArray = json_decode($FacebookGraphJSON); 
var_dump($dataArray); 
exit; 
+0

它不工作。給出以下錯誤: [function.file-get-contents]:無法打開流:HTTP請求失敗! HTTP/1.0 400錯誤請求 – user2302780 2013-04-21 11:34:17

+0

您需要在'define('ACCESS_TOKEN'')行中添加access_token。另請參見https://developers.facebook.com/docs/reference/api/search/:所有Graph API搜索查詢需要使用access_token = 參數傳入的訪問令牌。您需要的訪問令牌類型取決於您正在執行的搜索類型。 – 2013-04-21 12:01:04