Last active
August 29, 2015 14:02
-
-
Save dukeofgaming/a8fcf36bccef811fb7f2 to your computer and use it in GitHub Desktop.
Single function REST request 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 | |
set_time_limit(900); | |
function api_request($url, $method = null, $data = null, &$result_http_code = null){ | |
$curl = curl_init(); | |
if($method === null){ | |
$method = $_SERVER['REQUEST_METHOD']; | |
} | |
curl_setopt_array($curl, array( | |
CURLOPT_CUSTOMREQUEST => $method, | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_SSL_VERIFYPEER => false, | |
CURLOPT_TIMEOUT => 900, | |
CURLOPT_HTTPHEADER => array( | |
'Content-Type: application/json' | |
), | |
CURLOPT_USERPWD => $_REQUEST['user'].':'.$_REQUEST['password'], | |
CURLOPT_URL => $_REQUEST['base_url'].$url | |
)); | |
if($data !== null){ | |
curl_setopt_array($curl, array( | |
CURLOPT_POSTFIELDS => $data, | |
CURLOPT_HTTPHEADER => array( | |
'Content-Type: application/json', | |
'Content-Length: '.strlen($data) | |
) | |
)); | |
} | |
$result = curl_exec($curl); | |
if($result === false){ | |
$result = curl_error($curl); | |
} | |
if($result_http_code !== null){ | |
$result_http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE); | |
} | |
return $result; | |
curl_close($curl); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment