我要說最有可能的答案,這是您需要通過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_token' => '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_token' => '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);
您是否獲得任何錯誤?它們對於診斷潛在問題非常有用。 –
請報告你在這裏得到的錯誤信息。如果以上代碼是您的確切代碼,那麼將會是PHP語法錯誤,因爲URL字符串周圍沒有引號。 – DaveRandom
我收到警告警告: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錯誤請求 –