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