The Weibo public number, QQ public number, maps and many third-party features are provided to developers in the form of HTTP APIs (applications). So, how about requests, maps and other third-party APIs? This time will need to use to curl, this article will introduce you to the PHP Curl Library application.
Curl Chinese is called a URL-based library of functions. Its main function is to use related functions to simulate protocol requests.
For example:
Simulate a table to send data in one-way address
Impersonate a form without verification code to complete user login
Upload a file to a remote server
Request some features provided by the remote server
... ...
Curl supports Dict, file, FTP, FTPS, Gopher, HTTP, HTTPS, IMAP, IMAPS, LDAP, Ldaps, POP3, pop3s, rtmp, RTSP, SMTP, Smtps, Telnet, and T FTP protocol.
Curl also supports HTTPS authentication, HTTP POST, htt pput, FTP upload (This can also be done via PHP ftp extension), HTTP forms-based upload, proxy, cookies and username + password Authentication.
First, the beginning of the Curl resource
This is only a sentence, that is, using the Curl_init function. This parameter method is passed to any parameter. Returns the operation resource for curl.
Because, we are in the back through the curl_setopt to curl the operation of the resource variable into the data.
Cases:
$ch = Curl_init ();
Second, the parameter set the request protocol address
The detailed use of the curl_setopt function is as follows:
| type |
Description |
| Function |
curl_setopt |
| Parameter 1 |
Curl Resource Variable |
| Parameter 2 |
Curl parameter Options |
| Parameter 3 |
Curl parameter value |
curlopt_url This parameter option specifies the URL address of the request.
curl_setopt ($ch, Curlopt_url, "http://www.php.cn");
Third, the parameter setting returns the request result
We want the curl request to return the corresponding result. We want to get the corresponding result, also need to set a parameter, this parameter name is: Curlopt_returntransfer.
The return value is 1 if required. The result that is returned after a request is not required can be set to 0.
curl_setopt ($ch, Curlopt_returntransfer, 1);
Iv. setting up sending data
If it is a GET request, we do not need to set the parameters to send. At the time of the POST request, we need to set the Send method as the Post method. and set the data sent.
The *curlopt_post * value is set to 1 using the Post method, and 0 is not using the Post method
curlopt_postfields Setting the data passed
The declaration uses the POST method to send curl_setopt ($ch, Curlopt_post, 1);//What data is sent curl_setopt ($ch, Curlopt_postfields, $data);
Five, other parameter information settings
If it is https sometimes we need to ignore the security certificate of HTTPS.
Curlopt_ssl_verifypeer and Curlopt_ssl_verifyhost two parameters to false the certificate is ignored.
curl_setopt ($ch, Curlopt_ssl_verifypeer, false); curl_setopt ($ch, Curlopt_ssl_verifyhost, false);
Curlopt_header This parameter also determines whether to handle HTTP header information, we do not want to receive processing, we can set this value to 0.
curl_setopt ($ch, Curlopt_header, 0);
In addition, we can set the request timeout time, the parameter is: Curlopt_timeout.
curl_setopt ($ch, Curlopt_timeout, 10);
There are many other parameters that require access to www.php.cn
VI. Execute or execute PHP curl get return results
In our third step, set the value to 1 in the Curlopt_returntransfer parameter. If the result of the execution has data. When executed with curl_exec, the result is returned to the $output variable.
$output = curl_exec ($ch);
Vii. Close Curl Resources
Close the Curl resource. Because of the resource type, we have repeatedly stressed that there is open on the closed.
If you do not need to use it, release the memory immediately after using Curl_close shutdown.
Curl_close ($ch);
Eight, put the above together
<?php//Initialize $ch = Curl_init ()///SET options, including Urlcurl_setopt ($ch, Curlopt_url, "http://www.php.cn"), curl_setopt ($ch, Curlopt_returntransfer, 1); curl_setopt ($ch, Curlopt_header, 0);//execute and get HTML document contents $output = curl_exec ($ch);// Release the curl handle Curl_close ($ch);//print the obtained data print_r ($output);? >