将cURL命令转换为cURL PHP

时间:2016-02-04 02:59:17

标签: php curl

curl -X GET --header "Accept: application/json" --header "authorization: <API token>" "https://api.clashofclans.com/v1/locations"

^那是我要转换的cURL命令。我试着在cURL上寻找教程,但无法理解它。我尝试的当前代码如下:

$ch = curl_init('https://api.clashofclans.com/v1/clans/%2399VY9JR8');

curl_setopt($ch,CURLOPT_HTTPHEADER,array('authorization: myToken','Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$result = curl_exec($ch);
print_r($result);
curl_close($ch);

尝试从API获取响应并发布,然后在此处阅读。

1 个答案:

答案 0 :(得分:0)

在get请求中,您只需要使用URL本身传递params,代码应该可以工作:

$ch = curl_init();
$url = "https://api.clashofclans.com";
$clansId= "2399VY9JR8";
$headers= array("authorization: myToken");
curl_setopt($ch, CURLOPT_URL, $url.'clans/'.$clansId);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
print_r($result);
curl_close($ch);