2013-04-20 51 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