《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學院為您提供精彩教程。