php中的等效curl请求

时间:2016-04-01 07:47:36

标签: php curl

我试图在php中获取curl -X GET --header "Accept: application/json" --header "authorization: Bearer <API token>" "https://api.clashofclans.com/v1/clans?name=%23P8PQ8VL2"

到目前为止,我想出了这个

     $ch = curl_init(); 

 curl_setopt($ch, CURLOPT_URL, "https://api.clashofclans.com/v1/clans?name=%23P8PQ8VL2");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
 curl_setopt($ch, CURLOPT_HEADER, TRUE);
 curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
 curl_setopt($ch, CURLOPT_HEADER, TRUE);
 curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  'authorization: Api key'
));

$result=curl_exec($ch);
curl_close($ch);
var_dump(json_decode($result, true));

这是我第一次在php中使用curl,提前感谢:)

2 个答案:

答案 0 :(得分:0)

您正在使用第二组选项覆盖标题。我会像这样建立头数组:

$headers = array();
$headers[] = "Accept: application/json";
$headers[] = "authorization: Bearer " . $yourAPItoken;
$headers[] = 'Content-Type: application/x-www-form-urlencoded; charset=utf-8';

发送数组:

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

答案 1 :(得分:0)

嘿,谢谢你,我得到了它的工作。我不得不使用

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);