PHP Curl is a good feature, the following collection of a few pieces of good clips
0. Basic examples
General Process:
$to _url=$_get[' url '];
Print_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 URL
curl_setopt ($ch, Curlopt_url, $to _url);
curl_setopt ($ch, Curlopt_returntransfer, 1);
curl_setopt ($ch, Curlopt_header, 0);
Executes and obtains HTML document content
$output = curl_exec ($ch);
$output =preg_replace ("#href =\", "http://in2.qq-ex.com/amazon.php?url=", "href=\", $output);
Release Curl handle
curl_close ($ch);
echo $output;
Specify proxy address
curl_setopt ($ch, Curlopt_proxy, ' 11.11.11.11:8080 ');
If necessary, provide username and password
curl_setopt ($ch, curlopt_proxyuserpwd, ' user:pass ');
1. Test whether the website is running normally
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 Val ID 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 the printing it to the browser.
curl_setopt ($ch, Curlopt_url, $url);
$data = curl_exec ($ch);
Curl_close ($ch);
return $data;
}
3, save all the pictures under a website
function GetImages ($html) {
$matches = array ();
$regex = ' ~yun_qi_img/(. &~i ';
Preg_match_all ($regex, $html, $matches);
foreach ($matches [1] as $img) {
saveimg ($img);
}
}
function Saveimg ($name) {
$url = ' yun_qi_img/'. $name. ' JPG ';
$data = Get_data ($url);
File_put_contents (' photos/'. $name. JPG ', $data);
}
$i = 1;
$l =;
while ($i < $l) {
$html = get_data (' Http://somedomain.com/id/'. $i. ');
GetImages ($html);
$i + + 1;
}
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);
$output = curl_exec ($ch);
Curl_close ($ch);
5. Send JSON data using curl
$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)))
;