Use curl in PHP to send requests (GET requests and POST requests), curlget

Source: Internet
Author: User
Tags php website

Use curl in PHP to send requests (GET requests and POST requests), curlget

Basic Process for sending requests using CURL

Follow these steps to use the PHP extension of CURL to send an HTTP request:

1. initialize the connection handle;

2. Set the CURL option;

3. Execute and obtain the result;

4. Release the VURL connection handle.

The following program snippet is a typical process of sending HTTP using CURL.

// 1. initialize $ ch = curl_init (); // 2. set options, including URL curl_setopt ($ ch, CURLOPT_URL, "http://www.devdo.net"); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ ch, CURLOPT_HEADER, 0 ); // 3. execute and obtain the HTML document content $ output = curl_exec ($ ch); if ($ output = FALSE) {echo "CURL Error :". curl_error ($ ch);} // 4. release curl handle curl_close ($ ch );

The above Code uses four functions

  • Curl_init () and curl_close () are both simple to initialize and close CURL connections.
  • Curl_exec () executes the CURL request. If no error occurs, the return value of this function is the data returned by the corresponding URL, Which is satisfied with the string. If an error occurs, this function returns FALSE. It should be noted that the full equal sign is used to determine whether the output is FALSE. This is to distinguish between null strings returned and errors.
  • The most important function in the CURL function library is curl_setopt (). It can customize HTTP requests by setting the options defined by the CURL function library. The preceding code snippet uses three important options:

① CURLOPT_URL specifies the request URL;

② Setting CURLOPT_RETURNTRANSFER to 1 indicates that the returned result of the curl_exec function executed later is the URL returned string, instead of redirecting the returned string to the standard output and returning TRUE;

③ If curlopt_header is set to 0, no HTTP header information is returned.

There are also a lot of options for CURL, you can go to the official PHP website (http://www.php.net/manual/en/function.curl-setopt.php) to view a list Of all options supported by CURL.

Obtain CURL request output information

After the curl_exec () function is executed, you can use the curl_getinfo () function to obtain information about the CURL request output. The sample code is as follows:

Curl_exec ($ ch); $ info = curl_getinfo ($ sh); echo 'get '. $ info ['url']. 'Consumed time '. $ info ['total _ time']. 'second ';

In the above Code, curl_getinfo returns an associated array containing the following data:

  • Url: network address.
  • Content_type: content encoding.
  • Http_code: HTTP status code.
  • Header_size: header size.
  • Request_size: Request size.
  • Filetime: the time when the file was created.
  • Ssl_verify_result: SSL verification result.
  • Redirect_count: Jump count.
  • Total_time: Total time consumed.
  • Namelookup_time: DNS query time.
  • Connect_time: the waiting time.
  • Pretransfer_time: Pre-transmission preparation time.
  • Size_uplpad: size of the uploaded data.
  • Size_download: 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: the start time of transmission.
  • Redirect_time: The redirection time.

The curl_getinfo () function also has an optional parameter $ opt. Some constants can be set through this parameter, corresponding to the above field. If the second parameter is set, only the specified information is returned. For example, if $ opt is set to CURLINFO_TOTAL_TIME, The curl_getinfo () function only returns total_time, that is, the total transmission time consumed. When you only need to pay attention to certain transmission information, setting the $ opt parameter makes sense.

Use CURL to send a GET request

To use CURL to send a GET request, the key to sending a GET request is to assemble a URL with the correct format. The request address and GET data are composed of a "?" The GET variable names and values are separated by "=". The GET names and values are connected. PHP provides a function specifically used to assemble GET requests and data parts-http_build_query. This function accepts an associated array and returns the GET request string described by the associated data. Using this function, combined with the general process of sending HTTP requests through CURL, we closed a function called doCurlGetRequest to send a GET request. The Code is as follows:

/*** @ Desc closes the curl call interface and the get request method. */Function doCurlGetRequest ($ url, $ data, $ timeout = 5) {if ($ curl = "" | $ timeout <= 0) {return false ;} $ url = $ url. '? '. Http_bustm_query ($ data); $ con = curl_init (string) $ url); curl_setopt ($ con, CURLOPT_HEADER, false); curl_setopt ($ con, CURLOPT_RETURNTRANSFER, true ); curl_setopt ($ con, CURLOPT_TIMEOUT, (int) $ timeout); return curl_exec ($ con );}

This function transmits the URL with the GET parameter assembled using http_build_query to the curl_init function, and then uses CURL to send an HTTP request.

Use CURL to send a POST request

You can use the CURLOPT_POSTFIELDS option provided by CURL to set this option to POST string data and put the request in the body. Similarly, we implemented a function called doCurlPostRequest to send a POST request. The Code is as follows:

/***** @ Desc encapsulate the curl call interface and the post Request Method **/function doCurlPostRequest ($ url, $ requestString, $ timeout = 5) {if ($ url = ''| $ requestString ='' | $ timeout <= 0) {return false;} $ con = curl_init (string) $ url); curl_setopt ($ con, CURLOPT_HEADER, false); curl_setopt ($ con, CURLOPT_POSTFIELDS, $ requestString); curl_setopt ($ con, CURLOPT_POST, true); curl_setopt ($ con, CURLOPT_RETURNTRANSFER, true); curl_setopt ($ con, CURLOPT_TIMEOUT, (int) $ timeout); return curl_exec ($ con );}

In the above Code, in addition to setting CURLOPT_POSTFIELDS, we also set CURL_POST to true to identify this request as a POST request. The GET data can also be transmitted in the POST request. You only need to assemble the GET request data in the URL.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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: info-contact@alibabacloud.com 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.