[#1]
romet [2015-04-20 22:30:23]
function curl($url, $fields = array(), $auth = false){
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_VERBOSE, 1);
curl_setopt($curl, CURLOPT_HEADER, 1);
if($auth){
curl_setopt($curl, CURLOPT_USERPWD, "$auth");
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
}
if($fields){
$fields_string = http_build_query($fields);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_BINARYTRANSFER, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $fields_string);
}
$response = curl_exec($curl);
$header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$header_string = substr($response, 0, $header_size);
$body = substr($response, $header_size);
$header_rows = explode(PHP_EOL, $header_string);
$header_rows = array_filter($header_rows, trim);
foreach((array)$header_rows as $hr){
$colonpos = strpos($hr, ':');
$key = $colonpos !== false ? substr($hr, 0, $colonpos) : (int)$i++;
$headers[$key] = $colonpos !== false ? trim(substr($hr, $colonpos+1)) : $hr;
}
foreach((array)$headers as $key => $val){
$vals = explode(';', $val);
if(count($vals) >= 2){
unset($headers[$key]);
foreach($vals as $vk => $vv){
$equalpos = strpos($vv, '=');
$vkey = $equalpos !== false ? trim(substr($vv, 0, $equalpos)) : (int)$j++;
$headers[$key][$vkey] = $equalpos !== false ? trim(substr($vv, $equalpos+1)) : $vv;
}
}
}
//print_rr($headers);
curl_close($curl);
return array($body, $headers);
}
list($d['body'], $d['headers']) = curl2('http://google.com', array(q => '123'));
//POST to google.com with POST var "q" as "123"
echo '
';
print_r($d);
---------
OUTPUT:
Array
(
[headers] => Array
(
[0] => HTTP/1.1 405 Method Not Allowed
[Allow] => GET, HEAD
[Date] => Mon, 20 Apr 2015 22:20:10 GMT
[Server] => gws
[Content-Length] => 1453
[X-Frame-Options] => SAMEORIGIN
[Alternate-Protocol] => 80:quic,p=1
[Content-Type] => Array
(
[0] => text/html
[charset] => UTF-8
)
[X-XSS-Protection] => Array
(
[1] => 1
[mode] => block
)
)
[body] => html>...etc
)