当前位置:Gxlcms > PHP教程 > php获取远程内容,能否只建立一次连接,多次使用.解决方法

php获取远程内容,能否只建立一次连接,多次使用.解决方法

时间:2021-07-01 10:21:17 帮助过:15人阅读

php 获取远程内容,能否只建立一次连接,多次使用.
例如:
$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));
不用每次创立一个连接,和关闭一个连接。

------解决方案--------------------
CURLOPT_CUSTOMREQUEST
------解决方案--------------------
PHP code
[User:root Time:14:07:31 Path:/home/liangdong/php]$ php file.php 
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

^_^ Hello,欢迎使用ThinkPHP
GET / HTTP/1.1
Host: localhost
Accept: */*
Connection:Keep-alive

[User:root Time:14:07:32 Path:/home/liangdong/php]$ cat file.php 

[User:root Time:14:07:43 Path:/home/liangdong/php]$                     

人气教程排行