Created
July 5, 2022 07:23
-
-
Save shahzaibkhan/7e4d3f4e430942d59fbda0658131282b to your computer and use it in GitHub Desktop.
callAPI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function callAPI($method, $url, $data){ | |
$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_CUSTOMREQUEST, "PUT"); | |
if ($data) | |
curl_setopt($curl, CURLOPT_POSTFIELDS, $data); | |
break; | |
default: | |
if ($data) | |
$url = sprintf("%s?%s", $url, http_build_query($data)); | |
} | |
// OPTIONS: | |
curl_setopt($curl, CURLOPT_URL, $url); | |
curl_setopt($curl, CURLOPT_HTTPHEADER, array( | |
'APIKEY: 111111111111111111111', | |
'Content-Type: application/json', | |
)); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); | |
// EXECUTE: | |
$result = curl_exec($curl); | |
if(!$result){die("Connection Failure");} | |
curl_close($curl); | |
return $result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment