Skip to content

Instantly share code, notes, and snippets.

@just1and0
Created May 22, 2019 20:27
Show Gist options
  • Save just1and0/b846309de3d41224c4c98cf6986e7cb6 to your computer and use it in GitHub Desktop.
Save just1and0/b846309de3d41224c4c98cf6986e7cb6 to your computer and use it in GitHub Desktop.
Make POST, GET, PUT request in php
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:
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment