简体   繁体   English

PHP cURL超时无效

[英]PHP cURL timeout is not working

I'm having a server issue. 我有服务器问题。 I'm running a local server (for developing) and I've changed my local server from MAMP to XAMPP. 我正在运行本地服务器(用于开发),我已将本地服务器从MAMP更改为XAMPP。 However, on XAMPP, the cURL option CURLOPT_TIMEOUT_MS or CURLOPT_CONNECTTIMEOUT_MS gives me the next error: 但是,在XAMPP上,cURL选项CURLOPT_TIMEOUT_MSCURLOPT_CONNECTTIMEOUT_MS给出了下一个错误:

Warning: curl_setopt() expects parameter 2 to be long, string given 警告:curl_setopt()期望参数2为long,给定字符串

Is this because of the PHP or cURL version? 这是因为PHP还是cURL版本? Maybe a configuration setting? 也许配置设置?

curl_setopt($this->ch, CURLOPT_CONNECTTIMEOUT_MS, 2500);

Additional information: 附加信息:

  • OSX 10.6.8 OSX 10.6.8
  • PHP 5.3.1 PHP 5.3.1
  • cURL 7.19.7 cURL 7.19.7

Thanks in advance. 提前致谢。


Edit: There seems to be some confusion about the error and the variable to set. 编辑:似乎有一些关于错误和要设置的变量的混淆。 The error states that parameter 2 is invalid. 该错误表明参数2无效。 Not parameter 3 . 不是参数3 So the CURLOPT_CONNECTTIMEOUT_MS seems to be the issue. 所以CURLOPT_CONNECTTIMEOUT_MS似乎是个问题。

curl_setopt($this->ch, CURLOPT_CONNECTTIMEOUT_MS, 2500);
            ^^^^^^^^^  ^^^^^^^^^^^^^^^^^^^^^^^^^  ^^^^
parameter:     #1                 #2               #3

Fun fact: var_dump(CURLOPT_CONNECTTIMEOUT_MS); 有趣的事实: var_dump(CURLOPT_CONNECTTIMEOUT_MS); displays string(25) "CURLOPT_CONNECTTIMEOUT_MS" . 显示string(25) "CURLOPT_CONNECTTIMEOUT_MS" Just like the error states, it's a string instead of a float. 就像错误状态一样,它是一个字符串而不是浮点数。

I had the same problem and this solved it for me. 我有同样的问题,这解决了我。 Just add this line to the top of your script. 只需将此行添加到脚本的顶部即可。 It defines the missing constant only when not defined yet. 它仅在尚未定义时定义缺失的常量。

if (!defined(CURLOPT_CONNECTTIMEOUT_MS)) define('CURLOPT_CONNECTTIMEOUT_MS', 156); if(!defined(CURLOPT_CONNECTTIMEOUT_MS))define('CURLOPT_CONNECTTIMEOUT_MS',156);

For some reason, CURLOPT_CONNECTTIMEOUT_MS is defined incorrectly in your version of PHP. 出于某种原因, CURLOPT_CONNECTTIMEOUT_MS在您的PHP版本中定义不正确。 I'd check with whatever distribution of PHP you're using and find out whether it's a general bug. 我会检查你正在使用的PHP的任何分布,并找出它是否是一般错误。

The correct value is 156 : you should be able to use this instead: 正确的值是156 :你应该能够使用它:

curl_setopt($this->ch, /*CURLOPT_CONNECTTIMEOUT_MS */ 156, 2500);

NB that you really really really need to add comments explaining why you're doing this. NB,你真的真的真的需要添加注释解释你为什么这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM