1、fopen方式
//訪問指定URL函數function access_url($url) if($url== ) return false; $fp = fopen($url, r ) or exit( Open url faild! if($fp){ while(!feof($fp)) { $file .= fgets($fp) . fclose($fp); return $file; }
2、file_get_contents方式 (打開遠程文件的時候會造成CPU飆升。file_get_contents其實也可以post)
//以post方式獲取url$data = array ( foo = bar $data = http_build_query($data);$opts[ http ] = array ( method = POST , header = Content-type:application/x-www-form-urlencodedrn . Content-Length: . strlen($data) . rn , content = $data$context = stream_context_create($opts);$html = file_get_contents( http://localhost/test.html , false, $context);echo $html;
3、curl方式
function curl_file_get_contents($durl){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $durl); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true) ; // 獲取數據返回 curl_setopt($ch, CURLOPT_BINARYTRANSFER, true) ; // 在啟用 CURLOPT_RETURNTRANSFER 時候將獲取數據返回 $data = curl_exec($ch); curl_close($ch); return $data; }
4、fsockopen方式(只能獲取網站主頁信息,其他頁面不可以)
$fp = fsockopen( www.example.com , 80, $errno, $errstr, 30); (!$fp) { echo $errstr ($errno) br / /n }else { $out= GET / HTTP/1.1/r/n $out.= Host: www.example.com/r/n $out.= Connection: Close/r/n/r/n fwrite($fp, $out); while (!feof($fp)) { echo fgets($fp, 128); fclose($fp); }
相關文章推薦:
php使用PHPMailer如何發送郵件(附代碼)
Ubuntu系統下如何來隱藏TP的入口文件
以上就是PHP如何訪問URL?php訪問URL的方法總結(代碼)的詳細內容,PHP教程
鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。
新聞熱點
疑難解答