Curl is simple and practical

Source: Internet
Author: User

Study Address:https://yq.aliyun.com/articles/33262

simple steps to use Curl

to use curl to send URL requests, the steps are broadly divided into the following four stages:

1. Initialize
2. Set Request options
3. Perform a curl session and get related replies
4. Release the curl handle and close a curl session

output This is a get URL

The strength of curl is reflected in the second step. You can set the request options flexibly with curl_setopt, there are many options available

For details, refer to:http://cn2.php.net/manual/zh/function.curl-setopt.php

third, error handling

In the above code, you can also add code for error handling:

$response = curl_exec ($ch);

if ($response = = = FALSE) {

echo "CURL specific error message:". Curl_error ($ch);

}

Note that it is important to use = = = When making the above decision , because the requested reply may be an empty string, andcurl returns a false value in case of a request error , so we must use = = = instead of = =.

Iv. Obtaining specific information on Curl requests

After performing a curl request, you can also use Curl_getinfo to obtain specific information about the request:

Print out the array:

v. send a POST request using Curl

As we said earlier, when sending a GET request to a URL, it is not necessary to use curl to send a GET request, and you can use the more convenient file_get_contents function to complete the request. However, in general, when we submit a form, the data is submitted through the content area of the post request, not through the URL parameters, in which case we should use flexible curl to simulate sending the POST request.

Request Address

Http://www.chipshare.cn/wofans/index.php/Home/Test/post.html

The sample code is as follows :

as we can see,Curl successfully sends a POST request to post.php, submits some data, and receives the corresponding reply from post.php, and finally outputs a reply. The above example is simple, but it demonstrates the ease and power of curl sending a POST request, and you can make a fuss on the curl_setopt.

so we can encapsulate two general methods,get and post get information

Curl is simple and practical

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.