The use of curl in PHP

Source: Internet
Author: User
Tags usleep
with the version of the different curl in PHP to try also become different, for curl data, everyone is also confused, indeed about curl, online or can find some usage, but there is no explanation, today, I will find the information I have to share with you

First, there are some functions


This function returns a CURLM handle to is used as input to all of the other multi-functions, sometimes referred to as a Multi handle in some places in the documentation. This init call must has a corresponding call to Curl_multi_cleanup when the operation are complete .

This function returns a CURLM handle that will be used as input to all other multi-functions, which is passed as a parameter, which must have a corresponding function call Curl_multi_cleanup, when the operation is complete.


The function actually calls the underlying curl_multi_perform function of curl, and let's take a look at the description of the document to him

The simplest description is this;

Reads/writes available data from each easy handle

To translate into Chinese is to read or write from each of the handles to get the data

Here's a word.

This function handles transfers on all the added handles that need attention in an non-blocking fashion.

When an application have found out there's data available for the Multi_handle or a timeout have elapsed, the application sh Ould Call this function to read/write whatever there are to read or write right now Etc.curl_multi_perform returns as soon As the reads/writes is done. This function does isn't require that there actually are any data available for reading or so data can be written, It can be called just in case. It would write the number of handles that still transfer data in the second argument ' s integer-pointer.

If The amount of running_handles is changed from the previous call (or are less than the amount of easy handles yo U ' ve added to the multi handle), and you know that there are one or more transfers less "running". You can then call Curl_multi_info_read to get information on each individual completed transfer, and that returned info includes curlcode and more. If an added handle fails very quickly, it could never be counted as a running_handle.

When running_handles are set to zero (0) on the return of this function, there are no longer any transfers in Progr Ess.

Let's see what this passage says:

This function handles all the data transfer on the added handle, when a reference program discovers that there is data to be processed or the timeout occurs, the application should call this function, the function is read or write, the completion of the return, this function does not require that the data need to read or write, He can be called in any case, after calling to play this function, he will set the second argument (running-handles) passed to it, which indicates how many active connections there are

If Running-handles is called in the previous call, it means that the transmission is complete, or a transmission error has occurred, in order to see the transmission of each connection, you need to call the Curl-multi-read-info function. He will return an array containing three data, see the PHP Help documentation.

When Running-handles is set to 0 by curl, this means that all transmissions have been completed.

You may be confused about what the return value is, isn't it??

Curlmcode type, general Libcurl multi interface error code.

Before version 7.20.0:if you receive curlm_call_multi_perform, this basically means so you should call Curl_mu Lti_perform again, before you select () on the more actions. You don ' t has to does it immediately, but the return code means this libcurl may has more data available to return or that There may was more data to send off before it was "satisfied". Do note that Curl_multi_perform would returncurlm_call_multi_perform only if it wants to be called again IMM Ediately. When things was fine and there is nothing immediate it wants do, it ' ll return CURLM_OK and you need to the wait for "Action" and then call the This function again.

This function is only returns errors etc regarding the whole multi stack. Problems still might has occurred on individual transfers even if this function returns CURLM_OK. Use Curl_multi_info_read to figure out how individual transfers did.

Before 7.20.0, if you receive curlm_call_multi_perform, then it means that you should call the Curl_multi_perform function again before you call Curl_multi_select, In the absence of the data he has to deal with, he will return to CURLM_OK, you just have to wait for the action (temporarily understand the action, because I can not find the better word), if CURLM_OK is returned, then you just have to wait until (note), here is the curl we call _multi_select, he only returns a value of-1, no matter what, he will not wait for any action, but after usleep this time, Curl-multi-perform still will be called, may be meaningless, but there is no way


Let's talk about what this function does.

For the following code

while ($still _running && $result ==CURLM_OK)




$result =curl_multi_exec ($MH, $still _running);

}while ($result ==curlm_call_multi_perform);


In this code, if you don't have to curl_multi_select, you'll find that your CPU is outrageous, you can test it, it will affect your CPU efficiency, because he will continue to call this piece of meaningless code (in a certain period of time Yes)

This function is explained in the PHP Help document as

Block until there are active connections in the Curl batch connection

That is, if there is no data transmission detected, he will block, but you should note that in the current version of the following code is not applicable,

while ($still _running && $result ==CURLM_OK)


if (Curl_multi_select ($MH)!=-1)




$result =curl_multi_exec ($MH, $still _running);

}while ($result ==curlm_call_multi_perform);



Unfortunately, this code will fall into the loop , chasing its cause, you will find that Curl_multi_select only returns-1, that is, the contents of his inside has never been called, how to do it, at least when you see this code is not wrong

while ($still _running && $result ==CURLM_OK)


if (Curl_multi_select ($MH) ==-1)


Usleep (100);//This is the need to write your own, specific to your own, the official recommendation 100ms




$result =curl_multi_exec ($MH, $still _running);

}while ($result ==curlm_call_multi_perform);


So you can see that the server returned the data, pretty fast.


This function is clearly stated in the PHP Help documentation.

Queries whether a batch handle has a message or information returned in a separate transport thread. The message may contain a report such as an error code returned from a separate transport thread, or just a transmission line threads not completed.

Returns an array of related information when successful, and returns when it fails FALSE .

You just have to care about the return value.

As far as I can test, there are only two possible scenarios:

1. The transmission is complete.

2. Transmission Error

Array (3) {["MSG"]=> int (1) ["Result"]=> int (0) ["Handle"]=> resource (5) of type (curl)}

This is my result.

Curl_multi_getcontent ($res)

This function is much simpler.

CURLOPT_RETURNTRANSFERreturns the text stream of the obtained output if it is set

Note The $res parameter is a curl handle

We recommend a Web site below: is a detailed discussion of all aspects of curl, and of course, as a user, we just need to know how to use it,

This gives us a description of how to set the parameters of curl_setopt, after all, curl all the mystery lies in this

The above describes the use of curl in PHP, including aspects of the content, I hope the PHP tutorial interested in a friend to help.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.