php curl高并发_PHP: cURL - Manual

/*

Example values

url - 'http://example.com'

fields - array('var' => 'value'), or can be empty

auth - 'user:password', or can be empty

by romet, 4.20.2015

*/

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] => ...etc

)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值