Skip to content

Instantly share code, notes, and snippets.

@jezao
Last active March 31, 2017 20:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jezao/38e6f1ca8f7d8c21f541aae114d61f3c to your computer and use it in GitHub Desktop.
Save jezao/38e6f1ca8f7d8c21f541aae114d61f3c to your computer and use it in GitHub Desktop.
Make a GET/POST request with CURL in PHP
function simplehttp($method,$url,$data = [], $options = [],$headers=[])
{
$method = strtoupper($method);
$url = ( $method == 'GET' && sizeof($data) > 0) ? $url .= "?" . http_build_query($data) : $url;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if($method == 'POST') {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
}
foreach ( $options as $key => $value ) curl_setopt($ch, $key , $value);
if(sizeof($headers)>0) curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$output = curl_exec ($ch);
$info = curl_getinfo($ch);
curl_close ($ch);
return (object) [ 'success' => $info['http_code'] == 200 ,'http_code' => $info['http_code'],'info' => $info,'output' => $output];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment