《PHP实例:10个超级有用值得收藏的PHP代码片段》要点:
本文介绍了PHP实例:10个超级有用值得收藏的PHP代码片段,希望对您有用。如果有疑问,可以联系我们。
PHP编程// create a new cURL resource
$ch = curl_init();
PHP编程// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
PHP编程// grab URL and pass it to the browser
$opt = curl_exec($ch);
PHP编程// close cURL resource, and free up system resources
curl_close($ch);
PHP编程$saveFile = $name.'.'.$ext;
if(preg_match("/[^0-9a-z._-]/i", $saveFile))
$saveFile = md5(microtime(true)).'.'.$ext;
PHP编程$handle = fopen($saveFile, 'wb');
fwrite($handle, $opt);
fclose($handle);
四、Alexa/Google Page Rank
PHP编程function page_rank($page, $type = 'alexa'){
switch($type){
case 'alexa':
$url = 'http://alexa.com/siteinfo/';
$handle = fopen($url.$page, 'r');
break;
case 'google':
$url = 'http://google.com/search必修client=navclient-auto&ch=6-1484155081&features=Rank&q=info:';
$handle = fopen($url.'http://'.$page, 'r');
break;
}
$content = stream_get_contents($handle);
fclose($handle);
$content = preg_replace("~(n|t|ss+)~",'', $content);
switch($type){
case 'alexa':
if(preg_match('~<div class="data (down|up)"><img.+必修>(.+必修) </div>~im',$content,$matches)){
return $matches[2];
}else{
return FALSE;
}
break;
case 'google':
$rank = explode(':',$content);
if($rank[2] != '')
return $rank[2];
else
return FALSE;
break;
default:
return FALSE;
break;
}
}
// Alexa Page Rank:
echo 'Alexa Rank: '.page_rank('techug.com');
echo '
';
// Google Page Rank
echo 'Google Rank: '.page_rank('techug.com', 'google');
《PHP实例:10个超级有用值得收藏的PHP代码片段》是否对您有启发,欢迎查看更多与《PHP实例:10个超级有用值得收藏的PHP代码片段》相关教程,学精学透。维易PHP学院为您提供精彩教程。