2012-01-19 16 views
0

的file_get_contents不是使用下面的代碼使用批處理requests.Am從Facebook獲取FATA工作從Facebook獲取數據:的file_get_contents不工作的使用批次要求

$url='https://graph.facebook.com/?batch=[{ "method": "POST", "relative_url":"method/fql.query?query=SELECT+first_name+from+user+where+uid=12345678"}]& access_token=xxxxxxx&method=post'; 
echo $post = file_get_contents($url,true); 
it produces 
    Warning: file_get_contents(graph.facebook.com/?batch=[{ "method": "POST", "relative_url": "method/fql.query?query=SELECT+first_name+from+user+where+uid=12345"}]&access_to‌ ​ken=xxxx&method=post): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in /home/user/workspace/fslo/test.php on line 9 
+0

您是否獲得任何錯誤?它們對於診斷潛在問題非常有用。 –

+0

請報告你在這裏得到的錯誤信息。如果以上代碼是您的確切代碼,那麼將會是PHP語法錯誤,因爲URL字符串周圍沒有引號。 – DaveRandom

+0

我收到警告警告:file_get_contents(https://graph.facebook.com/?batch= [{「method」:「POST」,「relative_url」:「method/fql.query?query = SELECT + first_name + from + user +其中+ uid = 12345「}]&access_token = xxxx&method = post):無法打開流:HTTP請求失敗!第9行的/home/user/workspace/fslo/test.php中的HTTP/1.1 400錯誤請求 –

回答

5

我要說最有可能的答案,這是您需要通過urlencode()傳遞URL值 - 特別是JSON字符串。

此外,您應該是POST ing數據。

試試這個代碼:

注:我想你是從幾個變量建設的URL。如果你與你的實際代碼編輯的問題,我將使用代碼

<?php 

    $baseURL = 'https://graph.facebook.com/'; 

    $requestFields = array (
    'batch' => '[{"method":"POST","relative_url":"method/fql.query?query=SELECT+first_name+from+user+where+uid=12345678"}]', 
    'access_to‌ken' => 'whatever' 
); 
    $requestBody = http_build_query($requestFields); 

    $opts = array(
    'http'=>array(
     'method' => 'POST', 
     'header' => "Content-Type: application/x-www-form-urlencoded\r\n" 
       . "Content-Length: ".strlen($requestBody)."\r\n" 
       . "Connection: close\r\n", 
     'content' => $requestBody 
    ) 
); 

    $context = stream_context_create($opts); 

    $result = file_get_contents($baseURL, FALSE, $context); 

A「多標準」的方式做到這一點,這些天提供的解決方案是捲曲:

<?php 

    $baseURL = 'https://graph.facebook.com/'; 

    $requestFields = array (
    'batch' => '[{"method":"POST","relative_url":"method/fql.query?query=SELECT+first_name+from+user+where+uid=12345678"}]', 
    'access_to‌ken' => 'whatever' 
); 
    $requestBody = http_build_query($requestFields); 

    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $baseURL); 
    curl_setopt($ch, CURLOPT_POST, TRUE); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $requestBody); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/x-www-form-urlencoded', 
    'Content-Length: '.strlen($requestBody), 
    'Connection: close' 
)); 

    $post = curl_exec($ch); 
+0

$ requestBody = http_build_query($ requestBody); 或$ requestBody = http_build_query($ requestFields); –

+0

'$ requestBody = http_build_query($ requestFields);'是正確的 - 上面修正的答案。 – DaveRandom

+0

仍然不工作的朋友...它會產生一個錯誤:{「error」:{「message」:「頂級訪問令牌沒有爲批處理請求指定」,「type」:「GraphBatchException」}} –