Skip to content

Instantly share code, notes, and snippets.

@chowmean
Last active June 18, 2023 12:00
Show Gist options
  • Select an option

  • Save chowmean/0a3f733eccbb156a49a2 to your computer and use it in GitHub Desktop.

Select an option

Save chowmean/0a3f733eccbb156a49a2 to your computer and use it in GitHub Desktop.
PHP script to make get and post calls with different parameters using cURL
<?php
#author : chowmean
$data["test_data"]="just testing";
CallAPI("POST","uri",$data,"deviceId","accessToken","application/json","firefox");
# $data in json format
# $content_type in xxx-urlencoded-form / multipart data
# sending access token and deviceId in headers
function CallAPI($method, $url, $data = "",$device_id="",$access_token="",$content_type="",$user_agent="")
{ $curl = curl_init();
if ($content_type=="")
{$content_type='application/json'; }
switch ($method)
{
case "POST":{
curl_setopt($curl,CURLOPT_POST, 1);
if ($data!=""){
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);}
if($access_token!="" and $device_id!="")
{
curl_setopt($curl,CURLOPT_HTTPHEADER, array(
'Content-type:'.$content_type,
'Content-Length:'.strlen($data),
'User-Agent:'.$user_agent,
'accessToken:'.$access_token,
'deviceid:'.$device_id));
}
else
{
curl_setopt($curl,CURLOPT_HTTPHEADER, array(
'Content-type:'.$content_type,
'Content-Length:'.strlen($data),
'User-Agent:'.$user_agent ));
}
break;
}
default:{
if ($data){
$url = sprintf("%s?%s", $url, http_build_query($data)); //GET METHOD CASE
}
}
}
// Optional Authentication:
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
print_r($result);
}
?>
@Ragna667
Copy link

haki

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment