Today and third-party payment to do docking, when the local use Wamp (PHP version 5.4.14) to run their payment demo, reported a mistake. Null values cannot be passed in the Loadxml function. When I was troubleshooting the code, I found out that they used curl, and I've been in contact with curl before, but there's no deep research that just knows he's a tool that simulates the way a browser transmits data. Take this opportunity to take a good look at curl.
1. What is curl?
Baidu Encyclopedia to the explanation is: data transmission artifact. So where is it artifact? By finding the data, curl can use the syntax of the URL to simulate the browser to transfer data, because it is a simulated browser so it supports a variety of network protocols. HTTP, HTTPS, FTP, Gopher, Telnet, dict, file, and LDAP protocols are currently supported. Libcurl also supports HTTPS authentication, HTTP POST, HTTP PUT, FTP upload (This can also be done via PHP's FTP extension), HTTP forms-based uploads, proxies, cookies, and Username + password Authentication.
The most used in PHP is to simulate get and post requests via curl.
2. How to use Curl
(1), to use curl, the first step in PHP to open curl, in PHP configuration file php.ini found Extension=php_curl.dll, the front of the '; ' removed. Restart the server. With Phpinfo () printed in PHP code, it is found that curl in the diagram shows the success of the Open.
(2), using curl to complete a simple request is divided into four major strides:
1). Initialize, create a new curl resource.
2). Set the URL and the appropriate options
3). Fetch the URL and pass it to the browser
4). Turn off the curl resource. Release the resources.
1 $MyAES=NewMyaes ();2 $jiaRes=$MyAES->DESENCRYPTSTR ($data, "1102130405061708");3 $header[] = "Content-type:text/xml;charset=utf-8";4 $ch=curl_init ();5curl_setopt ($ch, Curlopt_url,reurl);6curl_setopt ($ch, Curlopt_returntransfer,true);7curl_setopt ($ch, Curlopt_httpheader,$header);8curl_setopt ($ch, Curlopt_post,true);9curl_setopt ($ch, Curlopt_postfields,$jiaRes);Ten $aa= Curl_exec ($ch); One //grab URL, and print A if(Curl_errno ($ch)){ - PrintCurl_error ($ch); - } theCurl_close ($ch);
Paste the project a piece of code, a good analysis.
Curl_init This is initialization, creating a new curl resource.
curl_setopt this is a function of PHP. Set a curl transfer option.
According to the PHP manual for the curl_setopt function, this function needs to pass three parameters, the first is a curl handle (my understanding is that the creation of the curl Resource). The second one is the CURLOPT_XX option that needs to be set. The third parameter is the value corresponding to the option.
The Curl_exec function is to perform a curl session. This function is initialized with a curl and all options are set before calling.
TRUE
FALSE
CURLOPT_RETURNTRANSFER
FALSE
。
Curlopt_url the URL address that needs to be obtained Curlopt_returntransfer returns the information obtained by CURL_EXEC () as a file stream, rather than a direct output.
Curlopt_httpheader array(‘Content-type: text/plain‘, ‘Content-length: 100‘)
Curlopt_post sends a regular POST request when enabled Curlopt_postfields All data is sent in the HTTP protocol post.
See the PHP documentation for settings corresponding to multiple curlopt http://php.net/manual/zh/book.curl.php
Specifically, curl_setopt_array- for the Curl Transfer session bulk setup options
Use Case for
1<?PHP2 //Create a new curl resource3 $ch=curl_init ();4 5 //set the URL and the appropriate options6 $options=Array(Curlopt_url = ' http://www.example.com/',7Curlopt_header =false8 );9 TenCurl_setopt_array ($ch,$options); One A //crawl the URL and pass it to the browser -Curl_exec ($ch); - the //Turn off the Curl resource and release the system resources -Curl_close ($ch); -?>
Curl is very powerful, for example, we just want to input the obtained content into a file, instead of outputting it directly to the browser, we can use the Curlopt_returntransfer option!
In this way, when Curl executes, it will output the contents of the page to the file, we can complete the collection and other functions!
In terms of efficiency, curl's principle is to simulate the operation of the browser, it is more efficient than file_get_contents () four times times higher than, imagine that we
To collect a very large amount of information on the station, operating efficiency of the difference imaginable!
Not only in terms of efficiency, but also in stability, curl is much better than the file_get_contents () function.
This is the powerful curl, the following will give you a detailed description of its advanced applications, we can simulate the browser post value, or even upload!
PHP Curl Usage Summary (i)