Introduction to data transmission CURL in PHP and data transmission curl
Check whether CURL extension is installed
Linux Command:
[root@fengniu020 ~]# php -i | grep -i curlAdditional .ini files parsed => /etc/php.d/curl.ini,curlcURL support => enabledcURL Information => 7.19.7
Analysis of curl operation steps:
CURL instance
1. A simple curl to capture the Baidu Homepage
2. Download a webpage and replace "Baidu" in the content with "Ferry ".
3. Call WebService
A simple curl that captures the Baidu Homepage
<?php$curl=curl_init('http://www.bkjia.com');curl_exec($curl);curl_close($curl);?>
Download a webpage and replace "Baidu" in the content with "Ferry ".
<? Php/*** instance Description: Download a web page on the network and replace "Baidu" with "Ferry" in the content to output */$ curlobj = curl_init (); // initialize curl_setopt ($ curlobj, CURLOPT_URL, "http://www.baidu.com"); // set URLcurl_setopt ($ curlobj, CURLOPT_RETURNTRANSFER, true) for webpage access ); // do not print out $ output = curl_exec ($ curlobj) directly after execution; // execute curl_close ($ curlobj); // close cURLecho str_replace ("Baidu", "Ferry ", $ output);?>
Call WebService
<? Php/*** instance Description: by calling WebService to query the current weather condition in Beijing *, the following interface is restricted for free users to access within 24 hours, information needs to be stored */$ data = 'thecityname = Beijing'; // $ data = 'thecityname = Beijing &'; // multiple connected $ curlobj = curl_init (); curl_setopt ($ curlobj, CURLOPT_URL, "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getWeatherbyCityName"); curl_setopt ($ curlobj, CURLOPT_HEADER, 0 ); curl_setopt ($ curlobj, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ curlobj, CURLOPT_PO ST, 1); // POST method curl_setopt ($ curlobj, CURLOPT_POSTFIELDS, $ data); curl_setopt ($ curlobj, CURLOPT_HTTPHEADER, array ("application/x-www-form-urlencoded; charset = UTF-8 "," Content-length :". strlen ($ data); // HTTP Request Header curl_setopt ($ curlobj, CURLOPT_USERAGENT, $ _ SERVER ['HTTP _ USER_AGENT ']); $ rtn = curl_exec ($ curlobj); if (! Curl_errno ($ curlobj) {// $ info = curl_getinfo ($ curlobj); // print_r ($ info); echo $ rtn;} else {echo 'curl error :'. curl_error ($ curlobj);} curl_close ($ curlobj);?>
Download an object from the FTP server to the Local Machine
<? Php/*** code instance-PHP-cURL practice * instance Description: download an object from the FTP server to the local machine */$ curlobj = curl_init (); curl_setopt ($ curlobj, CURLOPT_URL, "ftp: // 192.168.1.100/downloaddemo.txt"); curl_setopt ($ curlobj, CURLOPT_HEADER, 0); curl_setopt ($ curlobj, expires, 1); curl_setopt ($ curlobj, CURLOPT_TIMEOUT, 300); // times out after 300scurl_setopt ($ curlobj, CURLOPT_USERPWD, "peter. zhou: 123456 "); // FTP User name: Password // Sets up the outp Ut file $ outfile = fopen('dest.txt ', 'wb'); // The name of the local file saved to curl_setopt ($ curlobj, CURLOPT_FILE, $ outfile); $ rtn = curl_exec ($ curlobj ); fclose ($ outfile); if (! Curl_errno ($ curlobj) {// $ info = curl_getinfo ($ curlobj); // print_r ($ info); echo "RETURN :". $ rtn;} else {echo 'curl error :'. curl_error ($ curlobj);} curl_close ($ curlobj);?>
Upload local files to the FTP server
<? Php/*** code instance-PHP-cURL practice * instance Description: uploads a local file to the FTP server */$ curlobj = curl_init (); $ localfile = 'ftp01. php '; // the file to be uploaded $ fp = fopen ($ localfile, 'R'); curl_setopt ($ curlobj, CURLOPT_URL, "ftp: // 192.168.1.100/ftp01_uploaded.php "); // name of the uploaded file: curl_setopt ($ curlobj, CURLOPT_HEADER, 0); curl_setopt ($ curlobj, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ curlobj, CURLOPT_TIMEOUT, 300 ); // times out after 300scurl_setopt ($ cur Lobj, CURLOPT_USERPWD, "peter. zhou: 123456 "); // FTP User name: Password curl_setopt ($ curlobj, CURLOPT_UPLOAD, 1); curl_setopt ($ curlobj, CURLOPT_INFILE, $ fp ); // transfer the opened file curl_setopt ($ curlobj, CURLOPT_INFILESIZE, filesize ($ localfile); // the size of the uploaded file $ rtn = curl_exec ($ curlobj ); fclose ($ fp); if (! Curl_errno ($ curlobj) {echo "Uploaded successfully.";} else {echo 'curl error: '. curl_error ($ curlobj);} curl_close ($ curlobj);?>
Download an HTTPS resource on the network
<? Php/*** code instance-PHP-cURL practice * instance Description: download an HTTPS resource on the Network */$ curlobj = curl_init (); // initialize curl_setopt ($ curlobj, CURLOPT_URL, "https://ajax.aspnetcdn.com/ajax/jquery.validate/1.12.0/jquery.validate.js"); // set URLcurl_setopt ($ curlobj, CURLOPT_RETURNTRANSFER, true) for webpage access ); // do not print it out after execution // set HTTPS to support date_default_timezone_set ('prc'); // when using cookies, you must first set the time zone curl_setopt ($ curlobj, CURLOPT_SSL_VERIFYPEER, 0 ); // certificate Source Check whether the SSL encryption algorithm exists from the certificate. If it is set to 0, the slave server is terminated to verify curl_setopt ($ curlobj, CURLOPT_SSL_VERIFYHOST, 2 ); // $ output = curl_exec ($ curlobj); // execute curl_close ($ curlobj); // close cURLecho $ output;?>
In the above discussion, the data transmission CURL in PHP is all the content shared by xiaobian. I hope to give you a reference and support for the customer's house.