简体   繁体   中英

Equivalent curl request in php

Im trying to get curl -X GET --header "Accept: application/json" --header "authorization: Bearer <API token>" "https://api.clashofclans.com/v1/clans?name=%23P8PQ8VL2" in php

so far i came up with this

     $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));

this is the 1st time im using curl in php, thanks in advance :)

Your are overwriting the header with the second set of the options. I would build up the header array like so:

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

And send the array:

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

Hey thank you all i got it work. i had to use

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM