Last active
December 6, 2017 05:10
-
-
Save willwoodlief/1a008ab369ec48968d41d0cec1b9c4d6 to your computer and use it in GitHub Desktop.
General Curl Helper Function for PHP scripts
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 | |
/** | |
* @author Will Woodlief | |
* @license MIT | |
* | |
* General Curl Helper. Its multipurpose. Used it in the transcription project and now improved it | |
* @example curl_helper('cnn.com',null,$code) | |
* curl_helper('enri.ch',['var1'=>4],$code) | |
* @param $url string the url | |
* @param $fields array|null the params to pass | |
* @param &$http_code integer , will be set to the integer return code of the server. Its only an output variable | |
* @param $b_post boolean , default true . POST is true, GET is false | |
* @param $format string (json|xml|text) default json <p> | |
* Tells how the response is formatted, text means no conversion | |
* </p> | |
* @param $b_verbose boolean, default false. If set to true will print to screen the connection process | |
* @param $b_header_only boolean, default false <p> | |
* if true then no body is downloaded, and the return the headers | |
* </> | |
* @return array|string|int|null depends on the format and option | |
* @throws \Exception <p> | |
* if curl cannot connect or has error, | |
* if the site gives a response of 0 or greater than 500 (if $b_header_only is false) | |
* if the format is json and the the conversion has errors | |
* if the format is xml and the conversion has errors | |
* | |
*/ | |
function curl_helper($url, array $fields = null, &$http_code, $b_post=true , $format = 'json', $b_verbose=false, $b_header_only = false) | |
{ | |
$ch = curl_init(); | |
try { | |
curl_setopt_array($ch, [ | |
CURLOPT_RETURNTRANSFER => true, | |
]); | |
//curl will not print verbose info to the html browser screen. So we have to capture it and replay it | |
$verbose = null; | |
if ($b_verbose) { | |
$verbose = fopen('php://temp', 'w+'); | |
curl_setopt($ch, CURLOPT_STDERR, $verbose); | |
curl_setopt($ch, CURLOPT_VERBOSE, true); | |
} | |
if ($b_header_only) { | |
curl_setopt($ch, CURLOPT_HEADER, true); // we want headers | |
curl_setopt($ch, CURLOPT_NOBODY, true); // we don't need body | |
} | |
//if testing on localhost and url is https, then this gets around it because some localhost do not have ssl certs | |
if (isset($_SERVER['REMOTE_ADDR'])) { | |
if( in_array( $_SERVER['REMOTE_ADDR'], array( '127.0.0.1', '::1' ) ) ) { | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); | |
} | |
} | |
if ($b_post) { | |
curl_setopt($ch, CURLOPT_POST, 1); | |
if ($fields) { | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields); | |
} | |
} else { | |
if ($fields) { | |
$query = http_build_query($fields); | |
$url = $url . '?' . $query; | |
} | |
} | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); | |
$curl_output = curl_exec($ch); | |
if ($b_verbose) { | |
rewind($verbose); | |
$verboseLog = stream_get_contents($verbose); | |
echo "Verbose information:\n<pre>", htmlspecialchars($verboseLog), "</pre>\n"; | |
} | |
$http_code = intval(curl_getinfo($ch, CURLINFO_HTTP_CODE)); | |
if (curl_errno($ch)) { | |
throw new \Exception("could not open url: $url because of curl error: " . curl_error($ch)); | |
} else { | |
curl_close($ch); | |
} | |
if ($b_header_only) { | |
$header_array=array(); | |
$data=explode("\n",$curl_output); | |
$header_array['status']=$data[0]; | |
array_shift($data); | |
foreach($data as $part){ | |
$middle=explode(":",$part); | |
$header_array[trim($middle[0])] = trim($middle[1]); | |
} | |
return $header_array; //journey ends here with just the headers | |
} | |
if ($http_code == 0 || $http_code >= 500) { | |
throw new \Exception("Could not send data to $url, response was " . $http_code); | |
} | |
if (!is_string($curl_output) || !strlen($curl_output)) { | |
throw new \Exception("could not get contents from : $url "); | |
} | |
//makes it easy to skip formatting | |
if ($format === true || !$format) { | |
$format = 'none'; | |
} | |
switch ($format) { | |
case 'json': | |
//** @link https://gist.github.com/willwoodlief/9c431fbb55b376982b28cd20dc29d29f */ | |
$header_array = JsonHelpers::fromString($curl_output); | |
break; | |
case 'xml': | |
$header_array = simplexml_load_string($curl_output); | |
if ($header_array === null) { | |
throw new \Exception("failed to decode as xml: $curl_output"); | |
} | |
break; | |
default: { | |
$header_array = $curl_output; | |
} | |
} | |
return $header_array; | |
} | |
finally { | |
//always close curl, in case we need to do a lot of this. Its a limited resource | |
curl_close ($ch); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment