Created
June 18, 2020 14:17
-
-
Save codetgh/0d71ea27356d12342297dd61ae887e2a to your computer and use it in GitHub Desktop.
PHP Util class to call external REST web API
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 | |
class CurlClass { | |
private $url; | |
private $method; | |
private $data; | |
private $curlGlobal; | |
function __construct($url, $method, $data) { | |
$this->url = $url; | |
$this->method = $method; | |
$this->data = $data; | |
} | |
function apiCallReturn() { | |
$curl = curl_init(); | |
$this->curlGlobal = $curl; | |
switch ($this->method) { | |
case "POST": | |
curl_setopt($curl, CURLOPT_POST, 1); | |
if ($this->data) { | |
curl_setopt($curl, CURLOPT_POSTFIELDS, $this->data); | |
} | |
break; | |
case "GET": | |
curl_setopt($curl, CURLOPT_GET); | |
break; | |
default: | |
if ($this->data) { | |
$url = sprintf("%s?%s", $url, http_build_query($this->data)); | |
} | |
} | |
// Optional Authentication: | |
//curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); | |
//curl_setopt($curl, CURLOPT_USERPWD, "username:password"); | |
curl_setopt($curl, CURLOPT_URL, $this->url); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); | |
$result = curl_exec($curl); | |
return $result; | |
} | |
function connClose() { | |
curl_close($this->curlGlobal); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment