一、用file_get_contents函數(shù),以post方式獲取url

創(chuàng)新互聯(lián)服務(wù)項(xiàng)目包括潛山網(wǎng)站建設(shè)、潛山網(wǎng)站制作、潛山網(wǎng)頁制作以及潛山網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,潛山網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到潛山省份的部分城市,未來相信會繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
?php
$url=?'';
$data=?array('foo'=?'bar');
$data= http_build_query($data);
$opts=?array(
'http'=?array(
'method'=?'POST',
'header'="Content-type: application/x-www-form-urlencoded\r\n" ?.
"Content-Length: " ?.?strlen($data) .?"\r\n",
'content'=?$data
)
);
$ctx= stream_context_create($opts);
$html= @file_get_contents($url,'',$ctx);
二、用file_get_contents以get方式獲取內(nèi)容
?php
$url='';
$html=?file_get_contents($url);
echo$html;
?
三、用fopen打開url, 以get方式獲取內(nèi)容
?php
$fp=?fopen($url,'r');
$header= stream_get_meta_data($fp);//獲取報(bào)頭信息
while(!feof($fp)) {
$result.=?fgets($fp, 1024);
}
echo"url header: {$header} br":
echo"url body: $result";
fclose($fp);
?
四、用fopen打開url, 以post方式獲取內(nèi)容
?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\r\nCookie:cook1=c3;cook2=c4\r\n" ?.
"Content-Length: " ?.?strlen($data) .?"\r\n",
'content'=?$data
)
);
$context= stream_context_create($opts);
$html=?fopen(';id2=i4','rb',false,?$context);
$w=fread($html,1024);
echo$w;
?
五、使用curl庫,使用curl庫之前,可能需要查看一下php.ini是否已經(jīng)打開了curl擴(kuò)展
?php
$ch= curl_init();
$timeout= 5;
curl_setopt ($ch, CURLOPT_URL,?'');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT,?$timeout);
$file_contents= curl_exec($ch);
curl_close($ch);
echo$file_contents;
?
可以用以下4個方法來抓取網(wǎng)站 的數(shù)據(jù):
1. 用 file_get_contents 以 get 方式獲取內(nèi)容:
?
$url = '';
$html = file_get_contents($url);
echo $html;
2. 用fopen打開url,以get方式獲取內(nèi)容
?
$url = '';
$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函數(shù),以post方式獲取url
?
$data = array(
'foo'='bar',
'baz'='boom',
'site'='',
'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 = "";
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
echo $result;
4、使用curl庫,使用curl庫之前,可能需要查看一下php.ini是否已經(jīng)打開了curl擴(kuò)展
$url = '';
$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;
1.file_get_contents
PHP代碼
復(fù)制代碼 代碼如下:
?php
$url = "";
$contents = file_get_contents($url);
//如果出現(xiàn)中文亂碼使用下面代碼
//$getcontent = iconv("gb2312", "utf-8",$contents);
echo $contents;
?
2.curl
PHP代碼
復(fù)制代碼 代碼如下:
?php
$url = "";
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
//在需要用戶檢測的網(wǎng)頁里需要增加下面兩行
//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代碼
復(fù)制代碼 代碼如下:
?php
$handle = fopen ("", "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,設(shè)置
allow_url_fopen = On,allow_url_fopen關(guān)閉時fopen和file_get_contents都不能打開遠(yuǎn)程文件。
2.使用curl必須空間開啟curl。方法:windows下修改php.ini,將extension=php_curl.dll前面的分
號去掉,而且需要拷貝ssleay32.dll和libeay32.dll到C:\WINDOWS\system32下;Linux下要安裝curl擴(kuò)
展。
1、在當(dāng)前網(wǎng)頁echo出變量$_SERVER['HTTP_HOST']即可獲取域名或主機(jī)地址。
2、在當(dāng)前網(wǎng)頁echo出變量$_SERVER['PHP_SELF']即可獲取網(wǎng)頁地址。
3、在當(dāng)前網(wǎng)頁echo出變量$_SERVER["QUERY_STRING"]即可獲取網(wǎng)址參數(shù)。
4、在當(dāng)前網(wǎng)頁echo出變量$_SERVER['HTTP_REFERER']即可獲取用戶代理。
5、在當(dāng)前網(wǎng)頁echo出變量$_SERVER['HTTP_HOST']點(diǎn)上$_SERVER['REQUEST_URI']即可獲取完整的url,后面帶參數(shù)自己點(diǎn)上即可。
6、也是點(diǎn)上,用個變量保存好輸出。
用戶在表格form
中填寫數(shù)據(jù),然后提交到一個php文件,PHP文件使用函數(shù)獲取數(shù)據(jù)
form action="welcome.php" method="post"
Name: input type="text" name="name"br
E-mail: input type="text" name="email"br
input type="submit" value="提交"
/form用戶填寫完username后提交到welcome.php文件,在welcome.php文件中,
html
body
Welcome ?php echo $_POST["name"]; ?br
Your email address is: ?php echo $_POST["email"]; ?
/body
/html$_POST["name"]就是用戶輸入的名字
可以參考以下幾種方法:
方法一: file_get_contents獲取
span style="white-space:pre"?/span$url="";
span style="white-space:pre"?/span$fh= file_get_contents
('');span style="white-space:pre"?/spanecho $fh;
方法二:使用fopen獲取網(wǎng)頁源代碼
span style="white-space:pre"?/span$url="";
span style="white-space:pre"?/span$handle = fopen ($url, "rb");
span style="white-space:pre"?/span$contents = "";
span style="white-space:pre"?/spanwhile (!feof($handle)) {
span style="white-space:pre"??/span$contents .= fread($handle, 8192);
span style="white-space:pre"?/span}
span style="white-space:pre"?/spanfclose($handle);
span style="white-space:pre"?/spanecho $contents; //輸出獲取到得內(nèi)容。
方法三:使用CURL獲取網(wǎng)頁源代碼
$url="";
$UserAgent = 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.0.04506; .NET CLR 3.5.21022; .NET CLR 1.0.3705; .NET CLR 1.1.4322)';
$curl = curl_init();?//創(chuàng)建一個新的CURL資源
curl_setopt($curl, CURLOPT_URL, $url);?//設(shè)置URL和相應(yīng)的選項(xiàng)
curl_setopt($curl, CURLOPT_HEADER, 0);? //0表示不輸出Header,1表示輸出
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);?//設(shè)定是否顯示頭信息,1顯示,0不顯示。//如果成功只將結(jié)果返回,不自動輸出任何內(nèi)容。如果失敗返回FALSE
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_ENCODING, '');?//設(shè)置編碼格式,為空表示支持所有格式的編碼
//header中“Accept-Encoding: ”部分的內(nèi)容,支持的編碼格式為:"identity","deflate","gzip"。
curl_setopt($curl, CURLOPT_USERAGENT, $UserAgent);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
//設(shè)置這個選項(xiàng)為一個非零值(象 “Location: “)的頭,服務(wù)器會把它當(dāng)做HTTP頭的一部分發(fā)送(注意這是遞歸的,PHP將發(fā)送形如 “Location: “的頭)。
$data = curl_exec($curl);
echo $data;
//echo curl_errno($curl); //返回0時表示程序執(zhí)行成功
curl_close($curl);?//關(guān)閉cURL資源,并釋放系統(tǒng)資源
拓展資料
PHP(外文名:PHP: Hypertext Preprocessor,中文名:“超文本預(yù)處理器”)是一種通用開源腳本語言。語法吸收了C語言、Java和Perl的特點(diǎn),利于學(xué)習(xí),使用廣泛,主要適用于Web開發(fā)領(lǐng)域。PHP 獨(dú)特的語法混合了C、Java、Perl以及PHP自創(chuàng)的語法。它可以比CGI或者Perl更快速地執(zhí)行動態(tài)網(wǎng)頁。
用PHP做出的動態(tài)頁面與其他的編程語言相比,PHP是將程序嵌入到HTML(標(biāo)準(zhǔn)通用標(biāo)記語言下的一個應(yīng)用)文檔中去執(zhí)行,執(zhí)行效率比完全生成HTML標(biāo)記的CGI要高許多;PHP還可以執(zhí)行編譯后代碼,編譯可以達(dá)到加密和優(yōu)化代碼運(yùn)行,使代碼運(yùn)行更快。
參考資料:PHP(超文本預(yù)處理器)-百度百科
分享名稱:php獲取網(wǎng)頁數(shù)據(jù),php獲取網(wǎng)頁源代碼
標(biāo)題鏈接:http://chinadenli.net/article29/dsepdjh.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供電子商務(wù)、網(wǎng)站維護(hù)、自適應(yīng)網(wǎng)站、建站公司、企業(yè)網(wǎng)站制作、微信公眾號
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)