Confirm that the curl extension is installed
Linux under command:
[Root@fengniu020 ~]# Php-i | Grep-i Curl
Additional. ini files parsed =>/etc/php.d/curl.ini, Curl Curl support, =>
enabled
CURL Information => 7.19.7
Curl Operation Step Resolution:
Curl Instance
1. A simple curl, crawl Baidu home page
2. Download a Web page and the content of "Baidu" replaced by "ferry" after the output
3. Call WebService
A simple curl, crawl Baidu home page
<?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 the output
<?php
/**
* Example Description: Download a Web page on the network and the content of the "Baidu" replaced by the "ferry" after the output
* * * *
$curlobj = Curl_init (); Initialization of
curl_setopt ($curlobj, Curlopt_url, "http://www.baidu.com"); Sets the URL to access the Web page
curl_setopt ($curlobj, Curlopt_returntransfer, true); Do not print directly after execution
$output =curl_exec ($curlobj);//
Curl_close ($curlobj); Close Curl
echo str_replace ("Baidu", "Ferry", $output);
? >
Call WebService
<?php/** * Example Description: Through the call WebService query the current weather in Beijing * Below the interface, free users 24 hours access is limited, need to store information * * * $data =
' Thecityname= Beijing ';
$data = ' thecityname= Beijing & ';//multiple connected 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 the local
<?php
/**
* Code Instance-php-curl actual
case Description: Download a file from the FTP server to the 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 300s
curl_setopt ($curlobj, Curlopt_userpwd, "peter.zhou:123456");//ftp Username: password
//Sets up The output file
$outfile = fopen (' dest.txt ', ' WB ');//save to local filename
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
case Description: Upload the local file to the FTP server
/$curlobj = Curl_init ();
$localfile = ' ftp01.php ';//need to upload the file
$fp = fopen ($localfile, ' R ');
curl_setopt ($curlobj, Curlopt_url, "ftp://192.168.1.100/ftp01_uploaded.php");//file name saved after uploading
curl_setopt ($ Curlobj, Curlopt_header, 0);
curl_setopt ($curlobj, Curlopt_returntransfer, 1);
curl_setopt ($curlobj, Curlopt_timeout, 300); Times out after 300s
curl_setopt ($curlobj, Curlopt_userpwd, "peter.zhou:123456");//ftp username: password
curl_ Setopt ($curlobj, curlopt_upload, 1);
curl_setopt ($curlobj, Curlopt_infile, $fp);//Transfer Open File
curl_setopt ($curlobj, Curlopt_infilesize, FileSize ($ LocalFile))//uploaded 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 the network above an HTTPS resource
* * *
$curlobj = Curl_init (); Initialize
curl_setopt ($curlobj, Curlopt_url, "https://ajax.aspnetcdn.com/ajax/jquery.validate/1.12.0/ Jquery.validate.js "); Sets the URL to access the Web page
curl_setopt ($curlobj, Curlopt_returntransfer, true); Do not print directly after execution/
/Set up 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 authentication certificate from the certificate to check the existence of SSL encryption algorithm, set to 0 is to terminate from the server side of the verification
curl_setopt ($curlobj, Curlopt_ssl_verifyhost, 2);//
$ Output=curl_exec ($curlobj); Implementation of
Curl_close ($curlobj); Close Curl
echo $output;
? >
The above article discusses the data transmission in PHP Curl is a small series to share all the content, hope to give you a reference, but also hope that we support cloud habitat community.