Skip to content

Instantly share code, notes, and snippets.

@OneOfOne
Last active September 21, 2016 20:02
Show Gist options
  • Save OneOfOne/18b5fb89c12834bc1c26794799403235 to your computer and use it in GitHub Desktop.
Save OneOfOne/18b5fb89c12834bc1c26794799403235 to your computer and use it in GitHub Desktop.
a simple func to do curl json requests for those living in the stone age without php streams
<?php
function doCurlJSONReq($url, $data = null, $method = '', $returnString = false) {
//echo $url . "\n";
$ch = curl_init($url);
if($data !== null) {
if($method === '') $method = 'POST';
if(!is_string($data)) $data = json_encode($data);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-length: '.strlen($data)));
}
if($method !== '') curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$res = @curl_exec($ch);
curl_close($ch);
if($returnString) return $res;
$res = json_decode($res, true);
return is_array($res) ? $res : array();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment