tags:

views:

24

answers:

1

What does curl_multi_getcontent($ch) return if $ch isn't ready in PHP?

+2  A: 

The answer to the question you asked is

  • If the response has not been received yet, curl_multi_getcontent returns NULL.
  • If the transfer is in progress, curl_multi_getcontent returns the data retrieved so far.


The question you meant to ask, apparently, is this: How do I know when a curl_multi... operation is finished?

The answer is that you don't use curl_multi_getcontent for this. Instead, repeatedly call curl_multi_exec until the second parameter (which is the number of sub-handles still working on its transfer) goes to 0. This blog post, from the comments in the manual, shows some working code. You may also want to look at curl_multi_select, which will block until there's some activity on a connection. This probably leads to fewer cycles wasted making curl_multi_exec calls: See example #1 on the manual page for curl_multi_exec.

If you're using version 5.2.0 or later, you can use curl_multi_info_read to get the status of individual connections instead of waiting for all of them. (manual)

grossvogel
Hmm... I may have hit 'accept' a little too soon. It seems when I use the condition that curl_multi_getcontent($ch) !== NULL i get just the first several lines of the page and then it breaks. I assume it's still downloading and I get the information a little too early.
akellehe
As mentioned originally (and now further explained), you should be testing the result of `curl_multi_exec` to see if it's equal to `CURLM_CALL_MULTI_PERFORM`, before you even call `curl_multi_getcontent`.
grossvogel
What about when I want to know if a single handle in the multi-handle is finished?
akellehe
See the last sentence about using `curl_multi_info_read`? If you're running pre-5.2.0, then you might try calling `curl_getinfo` on the individual handle(s).
grossvogel