Data Transfer Curl Instance analysis in PHP

Source: Internet
Author: User
This article mainly introduces data transfer Curl example analysis in PHP, interested friends, I hope to help you.

Verify that the curl extension is installed

Linux under command:

[Root@fengniu020 ~]# Php-i | Grep-i curladditional. ini files parsed =/etc/php.d/curl.ini,curlcurl support = Enabledcurl information = 7. 19.7


Curl Operation Step Parsing:

Curl Instance

1. A simple curl, crawl Baidu homepage

2. Download a webpage and replace "Baidu" in the content with "ferry" after output

3. Call WebService

A simple curl, crawl Baidu homepage

<?php$curl=curl_init (' http://www.jb51.net '); curl_exec ($curl); Curl_close ($curl);? >

Download a webpage and replace "Baidu" in the content with "ferry" after output

<?php/** * Example Description: Download a webpage on the network and replace "Baidu" in the content with "ferry" after output */$curlobj = Curl_init ();      Initialize curl_setopt ($curlobj, Curlopt_url, "http://www.baidu.com");    Set the Urlcurl_setopt ($curlobj, Curlopt_returntransfer, true) to access the Web page;      Do not print directly after execution $output=curl_exec ($curlobj); Executive Curl_close ($curlobj);     Close Curlecho str_replace ("Baidu", "Ferry", $output); >

Call WebService

<?php/** * Example Description: By calling WebService query Beijing's current weather * below interface, free users within 24 hours of access is limited, need to store information */$data = ' thecityname= Beijing ';//$data = ' Thecityname= Beijing & ';//Multiple connections with & $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_post, 1); Post mode 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 a file from the FTP server to a local

<?php/** * Code Instance-php-curl actual Combat * Example Description: Download a file from the FTP server to local */$curlobj = Curl_init ();  curl_setopt ($curlobj, Curlopt_url, "ftp://192.168.1.100/downloaddemo.txt"); curl_setopt ($curlobj, Curlopt_header, 0); curl_setopt ($curlobj, Curlopt_returntransfer, 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 output file$ outfile = fopen (' dest.txt ', ' WB ');//Save to local file name 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 the local file to the FTP server

<?php/** * Code Instance-php-curl actual Combat * Example Description: Upload the local file to the FTP server */$curlobj = Curl_init ();  $localfile = ' ftp01.php ';//file to be uploaded $fp = fopen ($localfile, ' R '); curl_setopt ($curlobj, Curlopt_url, "ftp:// 192.168.1.100/ftp01_uploaded.php ");//File name curl_setopt ($curlobj, Curlopt_header, 0) after uploading; curl_setopt ($curlobj, Curlopt_returntransfer, 1); curl_setopt ($curlobj, Curlopt_timeout, 300); Times out after 300scurl_setopt ($curlobj, Curlopt_userpwd, "peter.zhou:123456");//ftp user name: Password curl_setopt ($curlobj, Curlopt_upload, 1); curl_setopt ($curlobj, Curlopt_infile, $fp);//Transfer Open File curl_setopt ($curlobj, Curlopt_infilesize, FileSize ($localfile));//upload file size $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 above the network

<?php/** * Code Instance-php-curl actual Combat * Example Description: Download an HTTPS resource above 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 the Urlcurl_setopt ($curlobj, Curlopt_returntransfer, true) to access the Web page;      Do not print directly after execution//set HTTPS support Date_default_timezone_set (' PRC '); When using cookies, you must first set the time zone curl_setopt ($curlobj, Curlopt_ssl_verifypeer, 0); Check the origin of the certificate verify that the SSL encryption algorithm exists from the certificate, set to 0 to terminate the validation from the server curl_setopt ($curlobj, Curlopt_ssl_verifyhost, 2); $output =curl_exec ($curlobj); Executive Curl_close ($curlobj);     Close Curlecho $output;? >

Summary: The above is the entire content of this article, I hope to be able to help you learn.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.