欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

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

程序员文章站 2024-02-14 22:12:10
...
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 OKDate: Thu, 10 May 2012 18:07:32 GMTServer: Apache/2.4.1 (Unix) PHP/5.4.0X-Powered-By: PHP/5.4.0Set-Cookie: PHPSESSID=qi60b6s1q783a4pnj81rscdet4; path=/Expires: Thu, 19 Nov 1981 08:52:00 GMTCache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0Pragma: no-cacheContent-Length: 257Keep-Alive: timeout=5, max=100Connection: Keep-AliveContent-Type: text/html; charset=utf-8
^_^ Hello,欢迎使用ThinkPHP
GET / HTTP/1.1Host: localhostAccept: */*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]$
php 获取远程内容,能否只建立一次连接,多次使用.解决方法

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。

相关文章

相关视频