Data transfer artifact Curl in PHP
First of all, thank you for the lesson network, teacher.
The concept of curl:
(client URL Library Function) a command-line tool that uses URL syntax to transfer data. A tool that clients request resources from the server.
usage Scenarios for curl:
Request webpage Resources (write web crawler);
WebService Data Interface resources (dynamic access to interface data, such as weather, number attribution, etc.);
FTP server inside the file resources (download and even upload files inside the FTP server);
Other resources (all resources on the network can be accessed and downloaded to by Curl).
use Curl in PHP:
1. Confirm that PHP supports curl;
(Windows php-i in the command line to view PHP-related information, and then right-click on the cmd window to edit find Curl,)
(Linux directly inside the PHP installation path/php-i |grep CURL);
2. There is, there is no installation, installation method before there is a summary.
3. Step: Initialize Curl_init (), send request, receive data curl_exec (), Turn off Curl Curl_close ()
make a simple web crawler with Curl
$curl = Curl_init ("http://www.baidu.com");
Curl_exec ($curl);
Curl_close ($curl);
Crawl Web page information with Curl and replace some content
$curlobj = Curl_init ();
curl_setopt ($curlobj, Curlopt_url, "http://www.baidu.com");
curl_setopt ($curlobj, Curlopt_returntransfer, 1);
$output = curl_exec ($curlobj);
Curl_close ($curlobj);
Echo str_replace (' Baidu ', ' hehe ', $output);
call WebService with curl to get weather information
Get the data by post
$data = "thecitycode=0376&theuserid=12";
$curlobj = Curl_init ();
curl_setopt ($curlobj, Curlopt_url, "Http://www.WebXml.com.cn/WebServices/WeatherWS.asmx/getWeather");
curl_setopt ($curlobj, Curlopt_header, 0);
curl_setopt ($curlobj, Curlopt_returntransfer, 1);
curl_setopt ($curlobj, Curlopt_post, 1);
curl_setopt ($curlobj, Curlopt_postfields, $data);
curl_setopt ($curlobj, Curlopt_httpheader, Array ("application/x-www-form-urlencoded;
CharSet = Utf-8 ",
"Content-length:". strlen ($data)));
$rtn = curl_exec ($curlobj);
if (!curl_error ($curlobj)) {
echo "RETURN:" $RTN;
} else {
echo "Curl Error:". Curl_error ($curlobj);
}
use Curl to download Web pages that need to be signed in
Save cookies, do two operations, the first step to save cookies to log in
$data = "Username = Yhy&password = 123456&remember = 1";
$curlobj = Curl_init ();
curl_setopt ($curlobj, Curlopt_url, "Http://www.imooc.com/user/login");
curl_setopt ($curlobj, Curlopt_returntransfer, 1);//Do not allow direct printing
Cookie-related settings, the medium in which cookies are used to store login information for websites
Date_default_timezone_set (' PRC ');//Set the time zone because the cookie has an expiration time
curl_setopt ($curlobj, curlopt_cookiession, TRUE);//support for cookies and session
curl_setopt ($curlobj, Curlopt_cookiefile, "cookiefile");
Save to local file, filename can be adjusted
curl_setopt ($curlobj, Curlopt_cookiejar, "cookiefile");
curl_setopt ($curlobj, Curlopt_cookie, Session_name (). ' = '. session_id ());
curl_setopt ($curlobj, Curlopt_header, 0);//Do not print header information
curl_setopt ($curlobj, curlopt_followlocation, 1);//support page link jump
curl_setopt ($curlobj, Curlopt_post, 1);//post Way
curl_setopt ($curlobj, Curlopt_postfields, $data);//Incoming data
curl_setopt ($curlobj, Curlopt_httpheader, Array ("application/x-www-form-urlencoded;
CharSet = Utf-8 ",
"Content-length:". strlen ($data)));//Length Information
Curl_exec ($curlobj);//execution
curl_setopt ($curlobj, Curlopt_url, "Http://www.imooc.com/space/index");//re-initialize
curl_setopt ($curlobj, curlopt_post, 0);//No POST mode
curl_setopt ($curlobj, Curlopt_httpheader, Array ("Content-type:text/xml");
$outfile = curl_exec ($curlobj);//execution
Curl_close ($curlobj);
Echo $output;
download files from FTP to local using Curl
$curlobj = Curl_init ();
curl_setopt ($curlobj, Curlopt_url, "ftp://target Ip/down.txt");
curl_setopt ($curlobj, Curlopt_header, 0);
curl_setopt ($curlobj, Curlopt_returntransfer, 1);
curl_setopt ($curlobj, Curlopt_timeout, 300);//time-out setting
curl_setopt ($curlobj, Curlopt_userpwd, "Ftpuser:password");//Set FTP account password
$outfile = fopen ("Dest.txt", "WB");//write to local file
curl_setopt ($curlobj, Curlopt_file, $outfile);
$rtn = curl_exec ($curlobj);
Fclose ($outfile);
if (!curl_error ($curlobj)) {
echo "RETURN:" $RTN;
} else {
echo "Curl Error:". Curl_error ($curlobj);
}
Curl_close ($curlobj);
upload files locally to the FTP server with Curl
$curlobj = Curl_init ();
$localfile = "./put.txt";
$fp = fopen ($localfile, ' R ');
curl_setopt ($curlobj, Curlopt_url, "ftp://target Ip/put01.txt");
curl_setopt ($curlobj, Curlopt_header, 0);
curl_setopt ($curlobj, Curlopt_returntransfer, 1);
curl_setopt ($curlobj, Curlopt_timeout, 300);//time-out setting
curl_setopt ($curlobj, Curlopt_userpwd, "Ftpuser:password");//Set FTP account password
curl_setopt ($curlobj, curlopt_upload, 1);
curl_setopt ($curlobj, Curlopt_infile, $fp);//Specify File
curl_setopt ($curlobj, Curlopt_infilesize, FileSize ($localfile));//Specify the size of the uploaded file
$rtn = curl_exec ($curlobj);
Fclose ($FP);
if (!curl_error ($curlobj)) {
echo "uploaded successfully";
} else {
echo "Curl error:". Curl_error ($curlobj);
}
Curl_close ($curlobj);
accessing HTTPS resources with Curl
$curlobj = Curl_init ();//initialization
curl_setopt ($curlobj, Curlopt_url, "Https://ajax.aspnetcdn.com/ajax/jquery.validate/1.12.0/jquery.validate.js") ;//Set access address
curl_setopt ($curlobj, Curlopt_returntransfer, 1);//do not print directly after execution
Date_default_timezone_set (' PRC ');//Set time zone
curl_setopt ($curlobj, Curlopt_ssl_verifypeer, 0);//terminating authentication from the server
$output = curl_exec ($curlobj);//execution
Curl_close ($curlobj);//Turn off Curl
Echo $output;
Using Curl in PHP