Skip to content

Instantly share code, notes, and snippets.

@schwindy
Created March 16, 2017 12:53
Show Gist options
  • Save schwindy/e5798405d0b6269945bdf037a58f6a4d to your computer and use it in GitHub Desktop.
Save schwindy/e5798405d0b6269945bdf037a58f6a4d to your computer and use it in GitHub Desktop.
PHP Curl Library
<?php
function app_curl_exec($ch, $args=[])
{
$debug = empty($args['debug'])?false:true;
if($debug)
{
curl_setopt($ch, CURLOPT_VERBOSE, true);
$verbose = fopen('php://temp', 'rw+');
curl_setopt($ch, CURLOPT_STDERR, $verbose);
}
$result = curl_exec($ch);
if($result === false)
{
echo __log("curl error:".curl_errno($ch).": %s<br>\n".htmlspecialchars(curl_error($ch)));
return false;
}
if($debug)
{
rewind($verbose);
$verboseLog = stream_get_contents($verbose);
echo "Verbose information:\n<pre>", htmlspecialchars($verboseLog), "</pre>\n";
}
curl_close($ch);
return $result;
}
function curl_get($url, $data=[], $bg=false)
{
$bg = $bg?"&":"";
if(!empty($data))$data = request_to_str($data);
$cmd = "curl --data '$data' $url $bg";
return exec($cmd);
}
function curl_post($url, $data='')
{
$curlDefault =
[
CURLOPT_URL => $url,
CURLOPT_PORT => 80,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $data
];
$handle = curl_init($url);
curl_setopt_array($handle, $curlDefault);
$result = app_curl_exec($handle);
return $result;
}
function curl_put($url, $data='', $headers=[])
{
$curlDefault =
[
CURLOPT_PORT => 80,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_ENCODING => '',
CURLOPT_HTTPHEADER => $headers,
CURLOPT_CUSTOMREQUEST => 'PUT',
CURLOPT_POSTFIELDS => $data
];
$handle = curl_init($url);
curl_setopt_array($handle, $curlDefault);
$result = app_curl_exec($handle);
return $result;
}
function curl_delete($url, $data='', $headers=[])
{
$curlDefault =
[
CURLOPT_PORT => 80,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_ENCODING => '',
CURLOPT_HTTPHEADER => $headers,
CURLOPT_CUSTOMREQUEST => 'DELETE',
CURLOPT_POSTFIELDS => $data
];
$handle = curl_init($url);
curl_setopt_array($handle, $curlDefault);
$result = app_curl_exec($handle);
return $result;
}
function request_to_str($data=[])
{
$str = "";
foreach($data as $key => $val)
{
if(is_array($val) || is_object($val))$val = json_encode(new __Object($val));
$str .= "$key=".$val."&";
}
return str_lreplace("&", "", $str);
}
function request_to_url($path, $data=[])
{
if(empty($data))$data = $_REQUEST;
if(empty($data) || $path === '/')return $path;
if(strpos($path, "?") === FALSE)$path .= "?";
else $path .= "&";
foreach($data as $key => $val)
{
if(is_array($val) || is_object($val))$val = json_encode($val);
$path .= "$key=".urlencode($val)."&";
}
return str_lreplace("&", "", $path);
}
function str_lreplace($search, $replace, $subject)
{
$pos = strrpos($subject, $search);
if($pos !== false)return substr_replace($subject, $replace, $pos, strlen($search));
return $subject;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment