Use of curl in php

Jul 29, 2016 am 09:15 AM
curl multi nbsp running

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.

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Solution: Your organization requires you to change your PIN Solution: Your organization requires you to change your PIN Oct 04, 2023 pm 05:45 PM

The message "Your organization has asked you to change your PIN" will appear on the login screen. This happens when the PIN expiration limit is reached on a computer using organization-based account settings, where they have control over personal devices. However, if you set up Windows using a personal account, the error message should ideally not appear. Although this is not always the case. Most users who encounter errors report using their personal accounts. Why does my organization ask me to change my PIN on Windows 11? It's possible that your account is associated with an organization, and your primary approach should be to verify this. Contacting your domain administrator can help! Additionally, misconfigured local policy settings or incorrect registry keys can cause errors. Right now

How to adjust window border settings on Windows 11: Change color and size How to adjust window border settings on Windows 11: Change color and size Sep 22, 2023 am 11:37 AM

Windows 11 brings fresh and elegant design to the forefront; the modern interface allows you to personalize and change the finest details, such as window borders. In this guide, we'll discuss step-by-step instructions to help you create an environment that reflects your style in the Windows operating system. How to change window border settings? Press + to open the Settings app. WindowsI go to Personalization and click Color Settings. Color Change Window Borders Settings Window 11" Width="643" Height="500" > Find the Show accent color on title bar and window borders option, and toggle the switch next to it. To display accent colors on the Start menu and taskbar To display the theme color on the Start menu and taskbar, turn on Show theme on the Start menu and taskbar

How to change title bar color on Windows 11? How to change title bar color on Windows 11? Sep 14, 2023 pm 03:33 PM

By default, the title bar color on Windows 11 depends on the dark/light theme you choose. However, you can change it to any color you want. In this guide, we'll discuss step-by-step instructions for three ways to change it and personalize your desktop experience to make it visually appealing. Is it possible to change the title bar color of active and inactive windows? Yes, you can change the title bar color of active windows using the Settings app, or you can change the title bar color of inactive windows using Registry Editor. To learn these steps, go to the next section. How to change title bar color in Windows 11? 1. Using the Settings app press + to open the settings window. WindowsI go to "Personalization" and then

OOBELANGUAGE Error Problems in Windows 11/10 Repair OOBELANGUAGE Error Problems in Windows 11/10 Repair Jul 16, 2023 pm 03:29 PM

Do you see "A problem occurred" along with the "OOBELANGUAGE" statement on the Windows Installer page? The installation of Windows sometimes stops due to such errors. OOBE means out-of-the-box experience. As the error message indicates, this is an issue related to OOBE language selection. There is nothing to worry about, you can solve this problem with nifty registry editing from the OOBE screen itself. Quick Fix – 1. Click the “Retry” button at the bottom of the OOBE app. This will continue the process without further hiccups. 2. Use the power button to force shut down the system. After the system restarts, OOBE should continue. 3. Disconnect the system from the Internet. Complete all aspects of OOBE in offline mode

How to enable or disable taskbar thumbnail previews on Windows 11 How to enable or disable taskbar thumbnail previews on Windows 11 Sep 15, 2023 pm 03:57 PM

Taskbar thumbnails can be fun, but they can also be distracting or annoying. Considering how often you hover over this area, you may have inadvertently closed important windows a few times. Another disadvantage is that it uses more system resources, so if you've been looking for a way to be more resource efficient, we'll show you how to disable it. However, if your hardware specs can handle it and you like the preview, you can enable it. How to enable taskbar thumbnail preview in Windows 11? 1. Using the Settings app tap the key and click Settings. Windows click System and select About. Click Advanced system settings. Navigate to the Advanced tab and select Settings under Performance. Select "Visual Effects"

Display scaling guide on Windows 11 Display scaling guide on Windows 11 Sep 19, 2023 pm 06:45 PM

We all have different preferences when it comes to display scaling on Windows 11. Some people like big icons, some like small icons. However, we all agree that having the right scaling is important. Poor font scaling or over-scaling of images can be a real productivity killer when working, so you need to know how to customize it to get the most out of your system's capabilities. Advantages of Custom Zoom: This is a useful feature for people who have difficulty reading text on the screen. It helps you see more on the screen at one time. You can create custom extension profiles that apply only to certain monitors and applications. Can help improve the performance of low-end hardware. It gives you more control over what's on your screen. How to use Windows 11

10 Ways to Adjust Brightness on Windows 11 10 Ways to Adjust Brightness on Windows 11 Dec 18, 2023 pm 02:21 PM

Screen brightness is an integral part of using modern computing devices, especially when you look at the screen for long periods of time. It helps you reduce eye strain, improve legibility, and view content easily and efficiently. However, depending on your settings, it can sometimes be difficult to manage brightness, especially on Windows 11 with the new UI changes. If you're having trouble adjusting brightness, here are all the ways to manage brightness on Windows 11. How to Change Brightness on Windows 11 [10 Ways Explained] Single monitor users can use the following methods to adjust brightness on Windows 11. This includes desktop systems using a single monitor as well as laptops. let's start. Method 1: Use the Action Center The Action Center is accessible

How to realize the mutual conversion between CURL and python requests in python How to realize the mutual conversion between CURL and python requests in python May 03, 2023 pm 12:49 PM

Both curl and Pythonrequests are powerful tools for sending HTTP requests. While curl is a command-line tool that allows you to send requests directly from the terminal, Python's requests library provides a more programmatic way to send requests from Python code. The basic syntax for converting curl to Pythonrequestscurl command is as follows: curl[OPTIONS]URL When converting curl command to Python request, we need to convert the options and URL into Python code. Here is an example curlPOST command: curl-XPOST https://example.com/api

See all articles