Created
September 22, 2019 23:22
-
-
Save mikemackintosh/47800189b1bc553e0621d12fcba7ee48 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
/************************************ | |
* | |
* arguments: | |
* $method is the requested method of the cURL, | |
* default GET | |
* $route is the URL to access | |
* default is blank | |
* $body is the body of the request for POST/PATCH/PUT, etc | |
* default is NULL | |
* $headers are custom headers to be passed to the request | |
* default is [] | |
* | |
* return: an array of response objects | |
* $code is the HTTP status code | |
* $headers contains the response headers | |
* $body contains the response body | |
* | |
***********************************/ | |
private static function makeRequest($method="GET", $route, $body=NULL, $headers=[]) { | |
// the default return object | |
$return = []; | |
// Exchange the auth code for an access token | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $route); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); | |
curl_setopt($ch, CURLOPT_HEADER, 1); | |
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0); | |
curl_setopt($ch, CURLOPT_TIMEOUT, 6); //timeout in seconds | |
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); | |
// Get the method and set if required | |
$method = strtoupper($method); | |
if( $method != "GET" ) { | |
curl_setopt($sh, CURLOPOT_CUSTOMREQUEST, $method); | |
} | |
// If method is a POST, set the appropriate headers | |
if( $method == "POST" ) { | |
curl_setopt($ch, CURLOPT_POST, true); | |
if ( !empty($body) ) { | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $body); | |
$headers[] = 'Content-Length: ' . strlen($body); | |
} | |
} | |
// Exec the curl command | |
$resp = curl_exec($ch); | |
// Evaluate the response code | |
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); | |
$return["code"] = $httpcode; | |
if( $httpcode === 0 ) { | |
throw new Exception("Connection to API failed with response code: {$httpcode}"); | |
return $return; | |
} | |
// Get the respone and set the return payload | |
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); | |
$return["headers"] = substr($resp, 0, $header_size); | |
$return["body"] = substr($resp, $header_size); | |
return $return; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment