---在底部更新,它与CURLOPT_COOKIE相关 -
我正在我的本地计算机上进行开发(192.168.1.103),我有一个PHP脚本,它进行CURL调用以获取标头和远程脚本返回的内容。
我已经安装了两份必须返回其内容的远程脚本:
- 在我的本地计算机上,在同一个虚拟主机下。 (http://192.168.1.103/test/output_script.php)
- 远程服务器上的一个。 (http://site.com/text/outputscript.php)
当我尝试从远程服务器获取内容时,CURL脚本运行良好,但在尝试从本地服务器获取内容时完全超时。
PHP CURL的详细信息是:
* About to connect() to 192.168.1.103 port 80 (#0)
* Trying 192.168.1.103... * connected
* Connected to 192.168.1.103 (192.168.1.103) port 80 (#0)
> GET /app/getContent HTTP/1.1
Host: 192.168.1.103
Accept: */*
Cookie: PHPSESSID=u8spbervheh3tcrv62gcnc2j72
* Operation timed out after 5001 milliseconds with 0 bytes received
* Closing connection #0
请注意,URI会使用以下.htaccess文件重新编写(在两个位置):
RewriteEngine on
RewriteBase /cms/client1/public_html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]
另请注意,我已激活重写日志并比较请求,以确保mod_rewrite操作在所有情况下都完全相同。 (我100%肯定这不是重写麻烦)
如果我尝试使用Ubuntu下的CURL应用程序获取文件,则效果很好:
$ curl -v --cookie PHPSESSID=u8spbervheh3tcrv62gcnc2j72 http://192.168.1.103/app/getContent
* About to connect() to 192.168.1.103 port 80 (#0)
* Trying 192.168.1.103... connected
* Connected to 192.168.1.103 (192.168.1.103) port 80 (#0)
> GET /app/getContent HTTP/1.1
> User-Agent: curl/7.21.0 (i686-pc-linux-gnu) libcurl/7.21.0 OpenSSL/0.9.8o zlib/1.2.3.4 libidn/1.18
> Host: 192.168.1.103
> Accept: */*
> Cookie: PHPSESSID=u8spbervheh3tcrv62gcnc2j72
>
< HTTP/1.1 403 Forbidden
< Date: Thu, 24 Feb 2011 21:40:17 GMT
< Server: Apache/2.2.16 (Ubuntu)
< X-Powered-By: PHP/5.3.3-1ubuntu9.3
< 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
< Vary: Accept-Encoding
< Content-Length: 82
< Content-Type: text/html; charset=UTF-8
<
* Connection #0 to host 192.168.1.103 left intact
* Closing connection #0
WT_AUTH non défini. (strictement aucune authentification actuellement en session)
403错误和WT_AUTH内容是我期望收到的,而不是我用PHP的超时。
如果在远程服务器上使用php curl,我也会得到相同的(通缉和正确)结果:
* About to connect() to site.com port 80 (#0)
* Trying 123.123.123.123... * connected
* Connected to site.com (123.123.123.123) port 80 (#0)
> GET /app/getContent HTTP/1.1
Host: site.com
Accept: */*
Cookie: PHPSESSID=u8spbervheh3tcrv62gcnc2j72
< HTTP/1.1 403 Forbidden
< Date: Thu, 24 Feb 2011 21:45:30 GMT
< Server: Apache/2.2.16 (Debian) DAV/2 SVN/1.6.12 mod_fcgid/2.3.6
< 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: 28
< Content-Type: text/html; charset=UTF-8
<
* Connection #0 to host site.com left intact
* Closing connection #0
如果我在浏览器中直接访问192.168.1.103/app/getContent,我也会得到同样的结果。
最后,我还确保getContent脚本通过将日志放入其中来实现。奇怪的是,如果我在16:45:00启动请求,并且超时发生在16:45:05,则getContent脚本中记录的数据将在16:45:05过时。所以就像CURL在“开放”状态下保持联系一样。当连接关闭时,允许php脚本启动。
我对它的任何想法都不适用于本地?
如果您想查看PHP代码,请参阅以下相关部分:
$ressource = curl_init();
curl_setopt($ressource, CURLOPT_URL, $destinationUrl);
curl_setopt($ressource, CURLOPT_VERBOSE, true);
$handle = fopen(FRAMEWORK_ROOT . DIRECTORY_SEPARATOR . 'log' . DIRECTORY_SEPARATOR . 'curl_debug.txt', 'w');
curl_setopt($ressource, CURLOPT_STDERR, $handle);
// Turn off the server and peer verification (TrustManager Concept).
curl_setopt($ressource, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ressource, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ressource, CURLOPT_RETURNTRANSFER, TRUE); //retourn content
curl_setopt($ressource, CURLOPT_HEADER, TRUE); //get HTTP headers
curl_setopt($ressource, CURLOPT_COOKIE, session_name() . '=' . session_id());
curl_setopt($ressource, CURLOPT_TIMEOUT, 5);
echo "\n<br />" . date('Y/m/d H:i:s');
$httpResponse = curl_exec($ressource);
echo "\n<br />" . date('Y/m/d H:i:s');
if(curl_errno($ressource) != 0)
throw new Core_Exc_Def(curl_error($ressource)); // WILL THROW AN ERROR ON 192.168.1.103, BUT NOT ON THE REMOTE SITE.
有趣的事实:在添加TIMEOUT之前,加载是无限的。本地网站没有响应,甚至其他页面。我需要重新启动apache服务器才能再次访问该站点...
更新:
如果我评论该行:
curl_setopt($ressource, CURLOPT_COOKIE, session_name() . '=' . session_id());
它正在“工作”(它导致另一个问题,但没有任何与超时有关)。 这两个脚本都在同一个虚拟主机上,并共享同一个会话,但是不应该创建一个CURL TimeOut?!
答案 0 :(得分:4)
这是因为会话被锁定以进行写入。当您尝试使用相同的session_id将脚本连接到同一服务器时,第二个脚本将等待,直到释放该会话锁。
您需要更改您在请求中发送的session_id
:
变化:
curl_setopt($ressource, CURLOPT_COOKIE, session_name() . '=' . session_id());
要:
curl_setopt($ressource, CURLOPT_COOKIE, session_name() . '=' . md5(session_id() . mktime()));