<?php
function curl_it($url = "", $first_call = true, $additional_header = NULL, $post = NULL, $put = NULL, $return_header = false, &$http_code = NULL, $basic_authentication = NULL, $proxy = NULL, $cookie = 'cookiefile', $follow_location = true, $timeout = 15)
{
if ($cookie == 'cookiefile')
$cookie = $_SERVER['DOCUMENT_ROOT'].'/cache/cookie/shop/cookie-curl_it.php';
$curl_max_loops = 5;
$html = $redirect_url = "";
static $curl_loop_counter = 0;
if ($first_call)
$curl_loop_counter = 0;
$curl_loop_counter++;
if ($curl_loop_counter >= $curl_max_loops)
return $html;
if (strlen($url) > 0)
{
$ch = curl_init();
if (!is_null($proxy))
{
curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_PROXY, constant(strtoupper($proxy).'_PROXY'));
curl_setopt($ch, CURLOPT_PROXYPORT, constant(strtoupper($proxy).'_PROXYPORT'));
curl_setopt($ch, CURLOPT_PROXYUSERPWD, constant(strtoupper($proxy).'_PROXYUSERPWD'));
}
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie); # SAME cookiefile
curl_setopt($ch, CURLOPT_ENCODING, '');
$header = array();
if (!is_null($additional_header) && is_array($additional_header))
$header = $additional_header;
if (!in_array('Pragma', $header)) $header[] = 'Pragma: no-cache';
if (!in_array('Cache-Control', $header)) $header[] = 'Cache-Control: no-cache';
if (!preg_grep('/^Accept\-Language:/i', $header)) $header[] = "Accept-Language: en-US,en;q=0.5";
if (!preg_grep('/^Expect:/i', $header)) $header[] = "Expect:"; //HTTP/1.1 100 Continue <- sucks
if (!preg_grep('/^Accept:/i', $header)) $header[] = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
if (!preg_grep('/^Accept\-Encoding/i', $header)) $header[] = "Accept-Encoding: gzip, deflate";
if (!preg_grep('/^User\-Agent:/i', $header)) $header[] = "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0";
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 8);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
if (!is_null($post))
{
curl_setopt($ch, CURLOPT_POST, 1);
//this is already a finished post string
//rawurlencode kills it!
//recurse($post, '$value=rawurlencode($value)');
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
}
if (!is_null($put))
{
if ($put == 'DELETE')
{
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
curl_setopt($ch, CURLOPT_POSTFIELDS, '');
}
elseif ($put == 'OPTIONS')
{
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'OPTIONS');
curl_setopt($ch, CURLOPT_POSTFIELDS, '');
}
else
{
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS, $put);
}
}
if (!is_null($basic_authentication))
{
//username:password
//nor username or password can be emtpy
if (preg_match('/.+:.+/', $basic_authentication))
curl_setopt($ch, CURLOPT_USERPWD, $basic_authentication);
}
curl_setopt($ch, CURLOPT_URL, $url);
$response = curl_exec($ch);
//remove proxy header
$pattern = '~^HTTP/1\.(0|1) 200 Connection established~i';
if (preg_match($pattern, $response))
{
$response = ltrim(preg_replace($pattern, '', $response));
$ar_pattern = array( '~^Transfer\-Encoding\: chunked~i',
'~^Proxy\-agent\: Online_Application~i',
'~^Connection: close~i');
foreach ($ar_pattern as $pattern)
{
if (preg_match($pattern, $response))
$response = ltrim(preg_replace($pattern, '', $response));
}
}
//list($header, $html) = explode("\r\n\r\n", $response, 2);
list($header, $html) = preg_split("/\R{2}/", $response, 2);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($follow_location && ($http_code == 301 || $http_code == 302 || $http_code == 307)) //permanently moved || found || temporary redirect
{
$matches = array();
//preg_match('/Location:(.*?)\n/', $header, $matches);
//$c_url = @parse_url(trim(array_pop($matches)));
$c_url = false;
if (preg_match('/Location:(.*?)($|\R)/', $header, $matches))
$c_url = parse_url(trim($matches[1]));
if ($c_url)
{
$last_url = parse_url(curl_getinfo($ch, CURLINFO_EFFECTIVE_URL));
if (!$c_url['scheme'])
$c_url['scheme'] = $last_url['scheme'];
if (!$c_url['host'])
$c_url['host'] = $last_url['host'];
if (!$c_url['path'])
$c_url['path'] = $last_url['path'];
if (!$c_url['fragment'])
$c_url['fragment'] = $last_url['fragment'];
$redirect_url = $c_url['scheme'].'://'.$c_url['host'].$c_url['path'].($c_url['query'] ? '?'.$c_url['query'] : '').($c_url['fragment'] ? '#'.$c_url['fragment'] : '');
}
}
curl_close($ch);
if (strlen($redirect_url) > 0) //redirect
$html = j_curl_it($redirect_url, false, $additional_header, $post, $put, $return_header, $http_code, $basic_authentication, $proxy, $cookie, $follow_location, $timeout);
}
return ($return_header ? $header."\r\n\r\n" : "").$html;
}