Skip to content

Instantly share code, notes, and snippets.

@DrARoberts
Created February 9, 2018 22:39
Show Gist options
  • Save DrARoberts/d1d044ed99dec060fe3758862119884a to your computer and use it in GitHub Desktop.
Save DrARoberts/d1d044ed99dec060fe3758862119884a to your computer and use it in GitHub Desktop.
Correct Implementation of PHP cURL for also sending field type $_FILES
if (!function_exists("getURIData")) {
/* function yonkURIData()
*
* Get a supporting domain system for the API
* @author Simon Roberts (Chronolabs) simon@labs.coop
*
* @return float()
*/
function getURIData($uri = '', $timeout = 25, $connectout = 25, $post = array(), $headers = array())
{
if (!function_exists("curl_init"))
{
die("Install PHP Curl Extension ie: $ sudo apt-get install php-curl -y");
}
$GLOBALS['php-curl'][md5($uri)] = array();
if (!$btt = curl_init($uri)) {
return false;
}
if (count($post)==0 || empty($post))
curl_setopt($btt, CURLOPT_POST, false);
else {
$uploadfile = false;
foreach($post as $field => $value)
if (substr($value , 0, 1) == '@' && !file_exists(substr($value , 1, strlen($value) - 1)))
unset($post[$field]);
else
$uploadfile = true;
curl_setopt($btt, CURLOPT_POST, true);
curl_setopt($btt, CURLOPT_POSTFIELDS, http_build_query($post));
if (!empty($headers))
foreach($headers as $key => $value)
if ($uploadfile==true && substr($value, 0, strlen('Content-Type:')) == 'Content-Type:')
unset($headers[$key]);
if ($uploadfile==true)
$headers[] = 'Content-Type: multipart/form-data';
}
if (count($headers)==0 || empty($headers))
curl_setopt($btt, CURLOPT_HEADER, false);
else {
curl_setopt($btt, CURLOPT_HEADER, true);
curl_setopt($btt, CURLOPT_HTTPHEADER, $headers);
}
curl_setopt($btt, CURLOPT_CONNECTTIMEOUT, $connectout);
curl_setopt($btt, CURLOPT_TIMEOUT, $timeout);
curl_setopt($btt, CURLOPT_RETURNTRANSFER, true);
curl_setopt($btt, CURLOPT_VERBOSE, false);
curl_setopt($btt, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($btt, CURLOPT_SSL_VERIFYPEER, false);
$data = curl_exec($btt);
$GLOBALS['php-curl'][md5($uri)]['http']['posts'] = $post;
$GLOBALS['php-curl'][md5($uri)]['http']['headers'] = $headers;
$GLOBALS['php-curl'][md5($uri)]['http']['code'] = curl_getinfo($btt, CURLINFO_HTTP_CODE);
$GLOBALS['php-curl'][md5($uri)]['header']['size'] = curl_getinfo($btt, CURLINFO_HEADER_SIZE);
$GLOBALS['php-curl'][md5($uri)]['header']['value'] = curl_getinfo($btt, CURLINFO_HEADER_OUT);
$GLOBALS['php-curl'][md5($uri)]['size']['download'] = curl_getinfo($btt, CURLINFO_SIZE_DOWNLOAD);
$GLOBALS['php-curl'][md5($uri)]['size']['upload'] = curl_getinfo($btt, CURLINFO_SIZE_UPLOAD);
$GLOBALS['php-curl'][md5($uri)]['content']['length']['download'] = curl_getinfo($btt, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
$GLOBALS['php-curl'][md5($uri)]['content']['length']['upload'] = curl_getinfo($btt, CURLINFO_CONTENT_LENGTH_UPLOAD);
$GLOBALS['php-curl'][md5($uri)]['content']['type'] = curl_getinfo($btt, CURLINFO_CONTENT_TYPE);
curl_close($btt);
return $data;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment