例如:
$ch = curl_init();
$timeout = 30;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$contents = trim(curl_exec($ch));
curl_close($ch);目标地址的url 是稳定的,仅传递的参数有不同,
能否代码里仅为
$timeout = 30;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$contents = trim(curl_exec($ch));
不用每次创立一个连接,和关闭一个连接。
$ch = curl_init();
$timeout = 30;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$contents = trim(curl_exec($ch));
curl_close($ch);目标地址的url 是稳定的,仅传递的参数有不同,
能否代码里仅为
$timeout = 30;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$contents = trim(curl_exec($ch));
不用每次创立一个连接,和关闭一个连接。
HTTP/1.1 200 OK
Date: Thu, 10 May 2012 18:07:32 GMT
Server: Apache/2.4.1 (Unix) PHP/5.4.0
X-Powered-By: PHP/5.4.0
Set-Cookie: PHPSESSID=qi60b6s1q783a4pnj81rscdet4; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Content-Length: 257
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/html; charset=utf-8<div style="font-weight:normal;color:blue;float:left;width:345px;text-align:center;border:1px solid silver;background:#E8EFFF;padding:8px;font-size:14px;font-family:Tahoma">^_^ Hello,欢迎使用<span style="font-weight:bold;color:red">ThinkPHP</span></div>
GET / HTTP/1.1
Host: localhost
Accept: */*
Connection:Keep-alive[User:root Time:14:07:32 Path:/home/liangdong/php]$ cat file.php
<?php
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://localhost');
curl_setopt($curl, CURLOPT_FORBID_REUSE, false);
curl_setopt($curl, CURLOPT_FRESH_CONNECT, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLINFO_HEADER_OUT, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Connection:Keep-alive'));
$res = curl_exec($curl);
if ($res !== false) {
echo $res . PHP_EOL;
$req = curl_getinfo($curl, CURLINFO_HEADER_OUT);
print_r($req);
}
?>
[User:root Time:14:07:43 Path:/home/liangdong/php]$ 这样,记住不要关闭curl资源,我是测试过的,可以循环使用。
好在你对这方面是有经验的,要是我就头疼了,总是弄错发送的数据格式