A common usage code example of a Curl library in PHP, a wave of curl
PHP Curl is a good feature, a few good pieces of the following collection
0. Basic examples
General Flow:
$to _url=$_get[' url '];p rint_r ($_get), if (substr ($to _url,0,1) = = '/') {$to _url= "http://www.amazon.com". $to _url;} echo $to _url;//Initialize $ch = Curl_init ();//SET options, including Urlcurl_setopt ($ch, Curlopt_url, $to _url); curl_setopt ($ch, Curlopt_ Returntransfer, 1); curl_setopt ($ch, Curlopt_header, 0);//execute and get HTML document contents $output = curl_exec ($ch); $output =preg_replace ("#href =\" # "," href=\ "http://in2.qq-ex.com/amazon.php?url=", $output);//Release curl handle Curl_close ($ch); echo $output;// Specify the proxy address curl_setopt ($ch, Curlopt_proxy, ' 11.11.11.11:8080 ');//if required, provide a username and password curl_setopt ($ch, Curlopt_proxyuserpwd, ' User:pass ');
1. Test whether the website is running properly
if (isdomainavailible (' http://gz.itownet.cn ')) { echo "up and running!"; } else { echo "woops, nothing found there."; } Returns true, if domain is availible, false if not function isdomainavailible ($domain) { //check, if a Vali D URL is provided if (!filter_var ($domain, Filter_validate_url)) { return false; } Initialize Curl $curlInit = Curl_init ($domain); curl_setopt ($curlInit, curlopt_connecttimeout,10); curl_setopt ($curlInit, curlopt_header,true); curl_setopt ($curlInit, curlopt_nobody,true); curl_setopt ($curlInit, curlopt_returntransfer,true); Get answer $response = curl_exec ($curlInit); Curl_close ($curlInit); if ($response) return true; return false;
2, can replace the operation of File_gecontents
function File_get_contents_curl ($url) { $ch = Curl_init (); curl_setopt ($ch, Curlopt_header, 0); curl_setopt ($ch, Curlopt_returntransfer, 1); Set Curl to return the data instead of printing it to the browser. curl_setopt ($ch, Curlopt_url, $url); $data = curl_exec ($ch); Curl_close ($ch);
3, save all the pictures under a website
function GetImages ($html) { $matches = array (); $regex = ' ~http://somedomain.com/images/(. *?) \.jpg~i '; Preg_match_all ($regex, $html, $matches); foreach ($matches [1] as $img) { saveimg ($img); }} function Saveimg ($name) { $url = ' http://somedomain.com/images/'. $name. JPG '; $data = Get_data ($url); File_put_contents (' photos/'. $name. JPG ', $data); } $i = 1; $l = 101; while ($i < $l) { $html = get_data (' Http://somedomain.com/id/'. $i. '/'); GetImages ($html);
4. FTP Application
Open a file pointer $file = fopen ("/path/to/file", "R"); The URL contains most of the info needed $url = "ftp://username:password@mydomain.com:21/path/to/new/file"; $ch = Curl_init (); curl_setopt ($ch, Curlopt_url, $url); curl_setopt ($ch, Curlopt_returntransfer, 1); Upload related options curl_setopt ($ch, curlopt_upload, 1); curl_setopt ($ch, Curlopt_infile, $fp); curl_setopt ($ch, Curlopt_infilesize, FileSize ("/path/to/file")); Set for ASCII mode (e.g. text files) curl_setopt ($ch, CURLOPT_FTPASCII, 1);
5. Use Curl to send JSON data
$data = Array ("name" = "Hagrid", "age" = "+"); $data _string = Json_encode ($data); $ch = Curl_init (' http://api.local/rest/users '); curl_setopt ($ch, Curlopt_customrequest, "POST"); curl_setopt ($ch, Curlopt_postfields, $data _string); curl_setopt ($ch, Curlopt_returntransfer, true); curl_setopt ($ch, Curlopt_httpheader, Array ( ' Content-type:application/json ', ' content-length: '. strlen ($ (data_string))) ;
http://www.bkjia.com/PHPjc/1123790.html www.bkjia.com true http://www.bkjia.com/PHPjc/1123790.html techarticle a wave of PHP in the common usage code example of the Curl Library, a wave of curl Php curl is a good feature, the following collection of a few good pieces 0, the basic example of the general flow: $to _url=$_get[' url ');p rin ...