Skip to content

Instantly share code, notes, and snippets.

@raihan-uddin
Created June 7, 2021 10:33
Show Gist options
  • Save raihan-uddin/6bd391d6c973aab80da9a2ba547abb76 to your computer and use it in GitHub Desktop.
Save raihan-uddin/6bd391d6c973aab80da9a2ba547abb76 to your computer and use it in GitHub Desktop.
<?php
// Your code here!
// Method: POST, PUT, GET etc
// Data: array("param" => "value") ==> index.php?param=value
function CallAPI($method, $url, $data = false)
{
$curl = curl_init();
switch ($method)
{
case "POST":
curl_setopt($curl, CURLOPT_POST, 1);
if ($data)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
case "PUT":
curl_setopt($curl, CURLOPT_PUT, 1);
break;
default:
if ($data)
$url = sprintf("%s?%s", $url, http_build_query($data));
}
// Optional Authentication:
$headr = array();
$headr[] = 'Content-length: 0';
$headr[] = 'Content-type: application/json';
$headr[] = 'X-Auth-Token: tnk7eP7Tugt1sF4wMIf0H0uIwYba69gLYBQI6JlxgFcFxmsDbF ';
curl_setopt($curl, CURLOPT_HTTPHEADER,$headr);
curl_setopt($curl, CURLOPT_POST,true);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
//curl_setopt($curl, CURLOPT_USERPWD, "username:password");
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
echo CallAPI("POST", "http://dims.itmapi.com/api/generic?page=1");
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment