[英]CURL call works in terminal, but it won't in PHP
我已經進行了廣泛的搜索,但是找不到與我的問題真正相關的任何主題,因此我決定提出一個新問題...
我正在嘗試使用CURL向Facebook Graph API發出批處理請求,該請求在Terminal中可以正常工作,但在PHP中根本無法使用...
重現步驟:
1.)從Graph API Explorer獲取自己的訪問令牌(無需權限,只需一個有效的令牌)鏈接: http : //developers.facebook.com/tools/explorer
2.)將其粘貼到以下終端命令中:(請注意,您總共需要將其粘貼到4個占位符中才能正常工作!)
curl \
-F 'access_token=PASTE_ACCESS_TOKEN_HERE' \
-F 'batch=[{"method":"GET","relative_url":"fql?q=SELECT+name+FROM+user+WHERE+uid%3Dme%28%29&access_token=PASTE_ACCESS_TOKEN_HERE"},{"method":"GET","relative_url":"fql?q=SELECT+name+FROM+user+WHERE+uid%3Dme%28%29&access_token=PASTE_ACCESS_TOKEN_HERE"},{"method":"GET","relative_url":"fql?q=SELECT+name+FROM+user+WHERE+uid%3Dme%28%29&access_token=PASTE_ACCESS_TOKEN_HERE"}]' \
https://graph.facebook.com
3.)在終端中執行它,它應該返回很多標頭,並且在主體部分(JSON編碼)中輸入您的名字4次。
4.)到目前為止,一切都應該正常進行。 不好的地方來了,在以下PHP腳本的4個占位符中再次粘貼令牌 :
<?php
$url = 'https://graph.facebook.com';
$fields = array(
'access_token' => 'PASTE_ACCESS_TOKEN_HERE',
'batch' =>
'[{"method":"GET","relative_url":"fql?q=SELECT+name+FROM+user+WHERE+uid%3Dme%28%29&access_token=PASTE_ACCESS_TOKEN_HERE"},{"method":"GET","relative_url":"fql?q=SELECT+name+FROM+user+WHERE+uid%3Dme%28%29&access_token=PASTE_ACCESS_TOKEN_HERE"},{"method":"GET","relative_url":"fql?q=SELECT+name+FROM+user+WHERE+uid%3Dme%28%29&access_token=PASTE_ACCESS_TOKEN_HERE"}]'
);
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
$fields_string = rtrim($fields_string, '&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
print_r($result);
?>
6.)運行代碼,您(至少我願意...)從Facebook得到以下錯誤消息:'{“ error”:{“ message”:“格式錯誤的訪問令牌ACCESS_TOKEN_HERE .... \\”} ]“,” type“:” OAuthException“,” code“:190}} 1'
我想Facebook認為JSON右花括號是訪問令牌的一部分,因此它將返回此錯誤。 我嘗試從$ fields ['batch']的末尾刪除轉義的雙引號和花括號,但是隨后引發另一個錯誤,指出batch參數必須是JSON字符串(顯然是...)
我是否想念某些東西,還是這是Facebook的錯(如果是,我不理解終端解決方案為何起作用...)?
這是官方的Facebook文檔BTW: http : //developers.facebook.com/docs/reference/api/batch
提前致謝!
編輯:我發現了一個(臨時)解決方法,它將正常工作(當然,因為它使用了shell腳本):
<?php
$result = shell_exec('curl \
-F \'access_token=PASTE_ACCESS_TOKEN_HERE\' \
-F \'batch=[{"method":"GET","relative_url":"fql?q=SELECT+name+FROM+user+WHERE+uid%3Dme%28%29&access_token=PASTE_ACCESS_TOKEN_HERE"},{"method":"GET","relative_url":"fql?q=SELECT+name+FROM+user+WHERE+uid%3Dme%28%29&access_token=PASTE_ACCESS_TOKEN_HERE"},{"method":"GET","relative_url":"fql?q=SELECT+name+FROM+user+WHERE+uid%3Dme%28%29&access_token=PASTE_ACCESS_TOKEN_HERE"}]\' \
https://graph.facebook.com');
print_r(json_decode($result),true);
?>
有人知道為什么這是比使用PHP-cURL更糟糕的解決方案(我想是)嗎? 我讀過某個地方,您不應該在PHP中使用Shell腳本,但我不知道為什么...
謝謝!
我運行了代碼的前半部分,只是為了查看$fields_string
的內容,然后再開始執行任何curl命令。 出於某種原因,rtrim並未為我刪除字符串中的最后一個與號。 也許用它代替刪除最后一個字符:
$fields_string = substr($fields_string, 0, -1);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.