In PHP programming, sometimes require a programmer to use Curl PHP extension to complete an HTTP request to send, for this operation, many beginners will encounter a lot of suffering, then the following to explain for you. Generally there are several steps to follow:
1. Initialize the connection handle;
2. Set the Curl option;
3. Implement and obtain results;
4. Release the Vurl connection handle.
The following program fragment is a typical process for sending HTTP using Curl
Four functions are used in the above code
· Curl_init () and Curl_close () are simple to initialize curl connections and turn off curl connections, respectively.
· Curl_exec () performs a curl request, and if no error occurs, the return of the function is the data returned by the corresponding URL, satisfied with the string, or False if an error occurs. It is important to note that it is the full equals sign to determine whether the output is false, in order to differentiate between returning an empty string and an error.
· The most important function in the Curl Library is curl_setopt (), which can customize the HTTP request by setting the options defined by the Curl function library. Three important options are used in the code snippet above:
1. CURLOPT_URL Specifies the URL of the request;
2. Curlopt_returntransfer set to 1 indicates that the return of the curl_exec function that is executed later is the return string of the URL instead of directing the return string to the standard output and returning true;
Curllopt_header set to 0 indicates that HTTP header information is not returned.
Get output information for a Curl request
After the curl_exec () function executes, you can use the Curl_getinfo () function to obtain information about the output of the curl request, as shown in the following example code:
In the code above, Curl_getinfo returns an associative array containing the following data:
· URL: Network address.
· Content_Type: Content encoding.
· Http_code:http status Code.
· The size of the header_size:header.
· Request_size: The size of the request.
· FILETIME: The time the file was created.
· SSL_VERIFY_RESULT:SSL validation results.
· Redirect_count: Jump Count.
· Total_time: Total time-consuming.
· Namelookup_time:dns query time-consuming.
· Connect_time: Wait for the connection to take time.
· Pretransfer_time: Preparation time before transmission.
· Size_uplpad: The size of the uploaded data.
· Size_download: The size of the downloaded data.
· Speed_download: Download speed.
· Speed_upload: Upload speed.
· Download_content_length: The length of the downloaded content.
· Upload_content_length: The length of the uploaded content.
· Starttransfer_time: Start the transfer schedule.
· Redirect_time: Redirection time-consuming.
The Curl_getinfo () function also has an optional parameter $opt, which allows you to set some constants that correspond to the field above, and if the second parameter is set, only the specified information is returned. For example, if you set $opt to Curlinfo_total_time, the Curl_getinfo () function returns only Total_time, that is, the total transfer time, and setting the $opt parameter is meaningful when you only need to focus on certain transmission information.
Send a GET request using curl
How to use curl to send a GET request, the key to sending a GET request is to assemble a properly formatted URL. Request address and get data by a "?" The name and value of the get variable are separated by "=", and each get name and value is connected by "&". PHP provides us with a function to assemble the GET request and Data section--http_build_query, which takes an associative array and returns a GET request string that is described by the associated data. Using this function, the general process of sending HTTP requests in conjunction with Curl, we closed a function--docurlgetrequest to send a GET request, with the following code:
Send a POST request using Curl
You can use the option Curlopt_postfields provided by Curl to set this option as the post string data to place the request in the body. We also implemented a function--docurlpostrequest to send a POST request, with the following code:
OK, now everyone should know how to operate it, if there is still a problem, you can consult a message. This article by the Professional Zhengzhou app development company Brigitte Xuan Science and Technology finishing release, original is not easy, if need reprint please indicate source.
Basic process for sending HTTP requests using curl in PHP programming