5

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. However, on XAMPP, the cURL option CURLOPT_TIMEOUT_MS or CURLOPT_CONNECTTIMEOUT_MS gives me the next error:

Warning: curl_setopt() expects parameter 2 to be long, string given

Is this because of the PHP or cURL version? Maybe a configuration setting?

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

Additional information:

  • OSX 10.6.8
  • PHP 5.3.1
  • 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. Not parameter 3. So the CURLOPT_CONNECTTIMEOUT_MS seems to be the issue.

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

Fun fact: var_dump(CURLOPT_CONNECTTIMEOUT_MS); displays string(25) "CURLOPT_CONNECTTIMEOUT_MS". Just like the error states, it's a string instead of a float.

10
  • Have you tried curl_setopt($this->ch, CURLOPT_CONNECTTIMEOUT_MS, 2500.0);? Commented Jan 30, 2012 at 11:01
  • Try to check for non printable character (non breakable space) between , and 2500 and if it's okay try using cast operator (int) before value. Commented Jan 30, 2012 at 11:02
  • @Sim that's double/float, not long, this shouldn't work. Commented Jan 30, 2012 at 11:02
  • @Sim Look at the error. It's about parameter 2 which is the CURLOPT_CONNECTTIMEOUT_MS constant. I don't think my problem lies in the value to set. Either way, I've tested it and it did not help. Thanks anyway. Commented Jan 30, 2012 at 11:04
  • Can you do a cast for the constant? As in (int)CURLOPT_CONNECTTIMEOUT_MS. I am not entirely sure if this is legit :) Commented Jan 30, 2012 at 11:09

2 Answers 2

8

For some reason, CURLOPT_CONNECTTIMEOUT_MS is defined incorrectly in your version of PHP. I'd check with whatever distribution of PHP you're using and find out whether it's a general bug.

The correct value is 156: you should be able to use this instead:

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for this, but can I ask how you came to know what the solution is? There are lots of questions, on SO and elsewhere, about CURL timeouts not working - is it possible that a similar fix could be the answer to CURLOPT_TIMEOUT not working? I and others have this. I can open a new question for this if you think that's the way to go. So to sum up, can I ask where the documentation is that provides the '156' in your answer?
@GeoffKendall Sorry, it's a long time ago that I wrote this answer. The comments above show the problem, but I can't remember what research I might have done. I got the value from my own system, by doing var_dump(CURLOPT_CONNECTTIMEOUT_MS); I don't think it's defined anywhere.
Thanks lonesome day, I did 'sniff the coffee' and google it - it's here laprbass.com/RAY_get_defined_constants.php and elsewhere. As it turned out the constants were fine for me, but it was worth a check.
8

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.