Last active
September 21, 2016 20:02
-
-
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
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 | |
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