Use of curl in php

WBOY
Release: 2016-07-29 09:15:06
Original
951 people have browsed it

With different versions, the trial use of curl in PHP becomes different. Everyone is confused about the information about curl. Indeed, you can still find some usage of curl on the Internet, but there is no explanation. Today, I will Let me share the information I found with everyone

First, some functions

curlm_multi_init

This function returns a CURLM handle to be used as input to all the other multi-functions, sometimes referred to as a multi handle in some places in the documentation. This init call MUST have a corresponding call to curl_multi_cleanup when the operation is complete.

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

curl_multi_exec

This function actually calls the underlying curl_multi_perform function of curl. Let’s take a look at the description of it in the document

The simplest description is this;

reads/writes available data from each easy handle

The translation into Chinese is; data can be obtained by reading or writing from each handle

There is a paragraph below

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

When an application has found out there's data available for the multi_handle or a timeout has elapsed, the application should call this function to read/write whatever there is to read or write right now etc.curl_multi_perform returns as soon as the reads/writes are done. This function does not require that there actually is any data available for reading or that data can be written, it can be called just in case. It will 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 is less than the amount of easy handles you've added to the multi handle), you know that there is one or more transfers less "running". You can then call curl_multi_info_read to get information about each individual completed transfer, and that returned info includes CURLcode and more. If an added handle fails very quickly, it may never be counted as a running_handle.

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

Let’s see what this passage actually says:

This function handles all data transfers on the added handle, when a referencing program finds that there is data that needs to be processed Or a timeout occurs, the application should call this function, which returns when reading or writing is completed. This function does not require that there is really data that needs to be read or written. It can be called under any circumstances. Call After playing with this function, it will set the second parameter passed to it (running-handles), which indicates how many active connections are left

If running-handles was called from the previous call, it means that the The transfer has been completed, or a transfer error occurred. In order to check the transfer status of each connection, you need to call the curl-multi-read-info function. It will return an array containing three data. Please see the PHP help documentation for details. .

When running-handles is set to 0 by curl, then it means that all transfers have been completed.

You may be confused about what its return value is, right? ?

CURLMcode type, general libcurl multi interface error code.

Before version 7.20.0: If you receive CURLM_CALL_MULTI_PERFORM, this basically means that you should call curl_multi_perform again, before you select() on more actions. You don't have to do it immediately, but the return code means that libcurl may have more data available to return or that there may be more data to send off before it is "satisfied". Do note that curl_multi_perform will returnCURLM_CALL_MULTI_PERFORM only when it wants to be called again immediately. When things are fine and there is nothing immediate it wants done, it'll return CURLM_OK and you need to wait for "action" and then call this function again.

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

Before version 7.20.0, if you receive CURLM_CALL_MULTI_PERFORM, it means that you should call the curl_multi_perform function again. Before you call curl_multi_select, when there is no data for it to process, it will return CURLM_OK, and you only need to wait for the action. (Let’s understand action as action for the time being, because I really can’t find a better word), if CURLM_OK is returned, then you only need to wait until (note), here is the curl_multi_select we called, he only returns the value -1, no matter what, he will not wait for any action, but after usleep, curl-multi-perform will still be called, which may not make sense, but there is no way

curl_multi_select

Let’s talk about the function of this function first

For the following code

while($still_running && $result==CURLM_OK)

{

do

{

$result= curl_multi_exec($mh,$still_running);

}while($result==CURLM_CALL_MULTI_PERFORM);

}

If you don’t use curl_multi_select in this code, you will find that your CPU is outrageous. You can Test it, this will affect your CPU usage efficiency, because it will continue to call this meaningless code (for a certain period of time)

This function is explained in the php help document as

Block until cURL batch There are active connections in the processing connection

That is to say, if no data transmission is detected, it will block, but you should pay attention, in the current version, the following code is not applicable,

while($still_running && $result==CURLM_OK)

{

if(curl_multi_select($mh)!=-1)

{

do

{

$result=curl_multi_ exec($mh,$still_running) ;

}while($result==CURLM_CALL_MULTI_PERFORM);

}

}

It’s really unfortunate that this code will fall into a dead loop. If you trace the reason, you will find that curl_multi_select only returns - 1. In other words, the content in it has never been called. What should you do? At least there will be no errors when you see this code

while($still_running && $result==CURLM_OK)

{

if(curl_multi_select($mh)==-1)

{

usleep(100);//This needs to be written by yourself. You can decide the details by yourself. The official recommendation is 100ms

}

do

{

​ $result=curl_multi_exec($mh,$still_running);

}while($result==CURLM_CALL_MULTI_PERFORM);

}

This way you will find that the server returns the data, barbaric Fast

curl_multi_info_read

This function is very clear in the PHP help document

Query the batch handle to see if there are messages or information returned in a separate transmission thread. Messages may contain reports such as error codes returned from individual transfer threads or simply whether the transfer thread has completed.


Returns an array of related information when successful, and returns

FALSE when failed.

You only need to care about the return value

According to my test, if there is a message, there are only two possible situations:

1. The transmission has been completed

2. There is an error in the transmission


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

If CURLOPT_RETURNTRANSFER is set, returns the text stream of the obtained output

Attention The $res parameter is a certain curl handle

The following is a website recommended to everyone:

http://curl.haxx.se/ All aspects of curl are discussed in detail here. Of course, as users, we only need to know Just how to use it,

Here we are introduced to how to set the parameters of curl_setopt. After all, all the mystery of curl lies in this


The above introduces the use of curl in PHP, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!