Created
September 2, 2016 00:34
-
-
Save fauzandotme/ee9a38042afb31c9b274f3b11f668707 to your computer and use it in GitHub Desktop.
Curl PHP
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
function curl($link, $cookie = 0, $post = 0, $show_header = true, $auth = 0, $auth_basic = true, $json = false, $referer = 0, $opts = 0) { | |
global $pauth; | |
if (!extension_loaded('curl') || !function_exists('curl_init') || !function_exists('curl_exec')) html_error('cURL isn\'t enabled or cURL\'s functions are disabled'); | |
$arr = explode("\r\n", $referer); | |
$header = array(); | |
if (count($arr) > 1) { | |
$referer = $arr[0]; | |
unset($arr[0]); | |
$header = array_filter(array_map('trim', $arr)); | |
} | |
if (is_array($cookie)) { | |
if (count($cookie) > 0) $cookie = CookiesToStr($cookie); | |
else $cookie = false; | |
} else { | |
$cookie = $cookie != false ? trim($cookie) : false; | |
} | |
$opt = array( | |
CURLOPT_SSL_VERIFYPEER => 0, | |
CURLOPT_SSL_VERIFYHOST => 0, CURLOPT_RETURNTRANSFER => 1, | |
CURLOPT_FOLLOWLOCATION => 0, CURLOPT_FAILONERROR => 0, | |
CURLOPT_FORBID_REUSE => 1, CURLOPT_FRESH_CONNECT => 1, | |
CURLOPT_USERAGENT => "Opera/9.80 (Windows NT 6.1; U; en-US) Presto/2.10.229 Version/11.61", | |
); | |
if ($show_header) { | |
$opt[CURLOPT_HEADER] = 1; | |
} | |
else { | |
$opt[CURLOPT_HEADER] = 0; | |
} | |
if ($referer != false) $opt[CURLOPT_REFERER] = $referer; | |
if ($cookie !== false) $opt[CURLOPT_COOKIE] = $cookie; | |
if ($auth) { | |
if($auth_basic) { | |
$opt[CURLOPT_HTTPAUTH] = CURLAUTH_BASIC; | |
$opt[CURLOPT_USERPWD] = base64_decode($auth); | |
} | |
else { | |
array_push($header, "Authorization: Bearer $auth"); | |
} | |
} | |
if ($json) { | |
array_push($header, "Content-Type: application/json"); | |
} | |
// Send more headers... | |
$headers = array("Accept-Language: en-us;q=0.7,en;q=0.3", "Accept-Charset: utf-8,windows-1251;q=0.7,*;q=0.7", "Pragma: no-cache", "Cache-Control: no-cache", "Connection: Close"); | |
if (count($header) > 0) $headers = array_merge($headers, $header); | |
$opt[CURLOPT_HTTPHEADER] = $headers; | |
if ($post != '0') { | |
$opt[CURLOPT_POST] = 1; | |
$opt[CURLOPT_POSTFIELDS] = is_array($post) ? formpostdata($post) : $post; | |
} | |
if (isset($_GET["useproxy"]) && !empty($_GET["proxy"])) { | |
$opt[CURLOPT_HTTPPROXYTUNNEL] = false; | |
$opt[CURLOPT_PROXY] = $_GET["proxy"]; | |
if ($pauth) $opt[CURLOPT_PROXYUSERPWD] = base64_decode($pauth); | |
} | |
$opt[CURLOPT_CONNECTTIMEOUT] = $opt[CURLOPT_TIMEOUT] = 120; | |
if (is_array($opts) && count($opts) > 0) foreach ($opts as $O => $V) $opt[$O] = $V; | |
$link = str_replace(array(" ", "\r", "\n"), array(" "), $link); | |
$ch = curl_init($link); | |
foreach ($opt as $O => $V) { // Using this instead of 'curl_setopt_array' | |
curl_setopt($ch, $O, $V); | |
} | |
$page = curl_exec($ch); | |
$info = curl_getinfo($ch); | |
$errz = curl_errno($ch); | |
$errz2 = curl_error($ch); | |
curl_close($ch); | |
if (!empty($opt[CURLOPT_PROXY])) $page = preg_replace("@^(HTTP/1\.[0-1] \d+ [^\r|\n]+)\r\n\r\n(HTTP/1\.[0-1] \d+ [^\r|\n]+)@i", "$2\r\ncURL: $1", $page, 1); // The "100 Continue" or the proxy response header can break some functions in plugins, let move and rename it... | |
if ($errz != 0) html_error("[cURL:$errz] $errz2"); | |
return $page; | |
} | |
//HTML Error | |
function html_error($msg) { | |
echo "<pre>$msg</pre>"; exit(); | |
} | |
//PRE | |
function pre($var,$not_exit = 1) { | |
echo "<pre>\n" . htmlspecialchars(print_r($var, true)) . "\n</pre>\n"; | |
if(!$not_exit) { | |
exit(); | |
} | |
} | |
//post array to data | |
function formpostdata($post=array()) { | |
$postdata = ""; | |
foreach ($post as $k => $v) { | |
$postdata .= "$k=$v&"; | |
} | |
// Remove the last '&' | |
$postdata = substr($postdata, 0, - 1); | |
return $postdata; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment