Last active
July 4, 2019 15:45
-
-
Save Matteo182/b55fc40af8b6393a9151954ef068caf6 to your computer and use it in GitHub Desktop.
use curl for call API in php
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
/****************************************** */ | |
// Funzione per richiamare le API | |
/****************************************** */ | |
// Method: POST, PUT, GET etc | |
// Data: array("param" => "value") ==> index.php?param=value | |
function CallAPI($method, $url, $data = false){ | |
$url_c = 'http://95.110.166.40/API/v1'.$url; | |
$curl = curl_init($url_c); | |
error_log('======> $url_c'); | |
error_log($url_c); | |
switch ($method) | |
{ | |
case "POST": | |
curl_setopt($curl, CURLOPT_POST, true); | |
if ($data) | |
curl_setopt($curl, CURLOPT_POSTFIELDS, $data); | |
break; | |
case "PUT": | |
curl_setopt($curl, CURLOPT_PUT, true); | |
break; | |
case "GET": | |
curl_setopt($curl, CURLOPT_HEADER, 0); | |
if ($data) | |
curl_setopt($curl, CURLOPT_HTTPHEADER, $data); | |
break; | |
default: | |
if ($data) | |
$url_c = sprintf("%s?%s", $url_c, 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_c); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | |
$result = curl_exec($curl); | |
curl_close($curl); | |
error_log('======> curl result'); | |
error_log($result); | |
return $result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment