可以參考以下幾種方法:
方法一: file_get_contents獲取
<span style="white-space:pre"></span>$url="http://www..com/";
<span style="white-space:pre"></span>$fh= file_get_contents
('http://www.hxfzzx.com/news/fzfj/');<span style="white-space:pre"></span>echo $fh;
拓展資料
PHP(外文名:PHP: Hypertext Preprocessor,中文名:「超文本預處理器」)是一種通用開源腳本語言。語法吸收了C語言、Java和Perl的特點,利於學習,使用廣泛,主要適用於Web開發領域。PHP 獨特的語法混合了C、Java、Perl以及PHP自創的語法。它可以比CGI或者Perl更快速地執行動態網頁。
用PHP做出的動態頁面與其他的編程語言相比,PHP是將程序嵌入到HTML(標准通用標記語言下的一個應用)文檔中去執行,執行效率比完全生成HTML標記的CGI要高許多;PHP還可以執行編譯後代碼,編譯可以達到加密和優化代碼運行,使代碼運行更快。
❷ php怎麼抓取其它網站數據
可以用以下4個方法來抓取網站 的數據:
1. 用 file_get_contents 以 get 方式獲取內容:
?
$url = 'http://localhost/test2.php';
$html = file_get_contents($url);
echo $html;
2. 用fopen打開url,以get方式獲取內容
?
$url = 'http://localhost/test2.php';
$fp = fopen($url, 'r');
stream_get_meta_data($fp);
$result = '';
while(!feof($fp))
{
$result .= fgets($fp, 1024);
}
echo "url body: $result";
fclose($fp);
3. 用file_get_contents函數,以post方式獲取url
?
$data = array(
'foo'=>'bar',
'baz'=>'boom',
'site'=>'www.jb51.net',
'name'=>'nowa magic');
$data = http_build_query($data);
//$postdata = http_build_query($data);
$options = array(
'http' => array(
'method' => 'POST',
'header' => 'Content-type:application/x-www-form-urlencoded',
'content' => $data
//'timeout' => 60 * 60 // 超時時間(單位:s)
)
);
$url = "http://localhost/test2.php";
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
echo $result;
4、使用curl庫,使用curl庫之前,可能需要查看一下php.ini是否已經打開了curl擴展
$url = 'http://localhost/test2.php?site=jb51.net';
$ch = curl_init();
$timeout = 5;
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
echo $file_contents;
❸ PHP 如何獲取到一個網頁的內容
1.file_get_contents
PHP代碼
復制代碼 代碼如下:
<?php
$url = "http://www.jb51.net";
$contents = file_get_contents($url);
//如果出現中文亂碼使用下面代碼
//$getcontent = iconv("gb2312", "utf-8",$contents);
echo $contents;
?>
2.curl
PHP代碼
復制代碼 代碼如下:
<?php
$url = "http://www.jb51.net";
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
//在需要用戶檢測的網頁里需要增加下面兩行
//curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
//curl_setopt($ch, CURLOPT_USERPWD, US_NAME.":".US_PWD);
$contents = curl_exec($ch);
curl_close($ch);
echo $contents;
?>
3.fopen->fread->fclose
PHP代碼
復制代碼 代碼如下:
<?php
$handle = fopen ("http://www.jb51.net", "rb");
$contents = "";
do {
$data = fread($handle, 1024);
if (strlen($data) == 0) {
break;
}
$contents .= $data;
} while(true);
fclose ($handle);
echo $contents;
?>
註:
1.
使用file_get_contents和fopen必須空間開啟allow_url_fopen。方法:編輯php.ini,設置
allow_url_fopen = On,allow_url_fopen關閉時fopen和file_get_contents都不能打開遠程文件。
2.使用curl必須空間開啟curl。方法:windows下修改php.ini,將extension=php_curl.dll前面的分
號去掉,而且需要拷貝ssleay32.dll和libeay32.dll到C:\WINDOWS\system32下;Linux下要安裝curl擴
展。
❹ PHP怎麼調用網頁
a.php
<?php
/*不知道你是想跳轉還是取回網頁內容後顯示
$type為TRUE時採用頁面跳轉方式
$type為FALSE時採用取回內容後顯示
*/
$type = TRUE;
if( isset( $_GET['url'] ) ){
$url = 'http://' . $_GET['url'];
if( $type ){
header("Location: $url");
}else{
$page = file_get_contents($url);
echo $page;
}
}else{
echo '未設置URL參數';
}
?>
❺ php如何獲取當前頁面url路徑
#測試網址: http://localhost/blog/testurl.php?id=5
//獲取域名或主機地址
echo $_server['http_host']."
"; #localhost
//獲取網頁地址
echo $_server['php_self']."
"; #/blog/testurl.php
//獲取網址參數
echo $_server["query_string"]."
"; #id=5
//獲取用戶代理
echo $_server['http_referer']."
";
//獲取完整的url
echo 'http://'.$_server['http_host'].$_server['request_uri'];
echo 'http://'.$_server['http_host'].$_server['php_self'].'?'.$_server['query_string'];
#http://localhost/blog/testurl.php?id=5
//包含埠號的完整url
echo 'http://'.$_server['server_name'].':'.$_server["server_port"].$_server["request_uri"];
#http://localhost:80/blog/testurl.php?id=5
//只取路徑
$url='http://'.$_server['server_name'].$_server["request_uri"];
echo dirname($url);
#http://localhost/blog
❻ 如何通過php獲取提交頁面的URL
在PHP的開發中我們經常會通過網址URL向另一個網頁傳遞參數的問題。在這個過程中我們首先需要獲取到當前頁面的URL,然後將URL中各個參數的值保存到變數中。整個過程較為簡單,主要涉及到$_SERVER的用法。
1、$_server['http_host'],作用:獲取網址域名,如(www.5ibobo.com,這是波波的一個博客,暫且做例子吧)。
2、$_SERVER["PHP_SELF"],作用:獲取網頁地址,如(/code/445.html)。
3、$_SERVER["QUERY_STRING"],作用:獲取網址URL參數,待會我們會在實例中用到。
4、$_SERVER["HTTP_REFERER"],作用:獲取用戶的代理。
❼ php抓取網頁源碼方法
可以使用file_get_content函數來獲取源代碼,你只需要把網站傳入這個函數,獲取後是一個字元串,你需要格式化代碼就可以了
❽ PHP中怎樣發送post請求並獲取網頁
$post='POST數據';
//初始化
$curl=curl_init('URL');
$header=array();
$header[]='User-Agent:Mozilla/5.0(WindowsNT6.1)AppleWebKit/537.36(KHTML,likeGecko)Chrome/42.0.2311.90Safari/537.36';
curl_setopt($curl,CURLOPT_HTTPHEADER,$header);
//不輸出header頭信息
curl_setopt($curl,CURLOPT_HEADER,0);
//保存到字元串而不是輸出
curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
//post數據
curl_setopt($curl,CURLOPT_POST,1);
//請求數據
curl_setopt($curl,CURLOPT_POSTFIELDS,$post);
//是否抓取跳轉後的頁面
curl_setopt($curl,CURLOPT_FOLLOWLOCATION,1);
$response=curl_exec($curl);
curl_close($curl);
echo$response;
❾ php獲取指定網頁內容
一、用file_get_contents函數,以post方式獲取url
<?php
$url='http://www.domain.com/test.php?id=123';
$data=array('foo'=>'bar');
$data= http_build_query($data);
$opts=array(
'http'=>array(
'method'=>'POST',
'header'=>"Content-type: application/x-www-form-urlencoded " .
"Content-Length: " .strlen($data) ." ",
'content'=>$data
)
);
$ctx= stream_context_create($opts);
$html= @file_get_contents($url,'',$ctx);
二、用file_get_contents以get方式獲取內容
<?php
$url='http://www.domain.com/?para=123';
$html=file_get_contents($url);
echo$html;
?>
三、用fopen打開url, 以get方式獲取內容
<?php
$fp=fopen($url,'r');
$header= stream_get_meta_data($fp);//獲取報頭信息
while(!feof($fp)) {
$result.=fgets($fp, 1024);
}
echo"url header: {$header} <br>":
echo"url body: $result";
fclose($fp);
?>
四、用fopen打開url, 以post方式獲取內容
<?php
$data=array('foo2'=>'bar2','foo3'=>'bar3');
$data= http_build_query($data);
$opts=array(
'http'=>array(
'method'=>'POST',
'header'=>"Content-type: application/x-www-form-
urlencoded Cookie:cook1=c3;cook2=c4 " .
"Content-Length: " .strlen($data) ." ",
'content'=>$data
)
);
$context= stream_context_create($opts);
$html=fopen('http://www.test.com/zzzz.php?id=i3&id2=i4','rb',false,$context);
$w=fread($html,1024);
echo$w;
?>
五、使用curl庫,使用curl庫之前,可能需要查看一下php.ini是否已經打開了curl擴展
<?php
$ch= curl_init();
$timeout= 5;
curl_setopt ($ch, CURLOPT_URL,'http://www.domain.com/');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT,$timeout);
$file_contents= curl_exec($ch);
curl_close($ch);
echo$file_contents;
?>
❿ PHP獲取網頁內容的幾種方法
簡單的收集下PHP下獲取網頁內容的幾種方法:
用file_get_contents,以get方式獲取內容。
用fopen打開url,以get方式獲取內容。
使用curl庫,使用curl庫之前,可能需要查看一下php.ini是否已經打開了curl擴展。
用file_get_contents函數,以post方式獲取url。
用fopen打開url,以post方式獲取內容。
用fsockopen函數打開url,獲取完整的數據,包括header和body。