简体   繁体   中英

Convert cURL command to cURL PHP

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

^ That's the cURL command I want to convert. I tried looking for tutorials on cURL but couldn't understand it. Current code I tried is as follows:

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

Trying to achieve getting response from API and posting then reading them here.

In get request you just have to pass the params with the URL itself, following code should work:

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

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