當前位置:首頁 » 編程語言 » php獲取html頁面

php獲取html頁面

發布時間: 2023-09-27 23:28:20

php 獲取JS執行過後的html

PHP是後台伺服器語言,只會被動的等待客戶端連接。
如果你想要獲取執行JS後的代碼,可以用ajax來實現。
寫一個JS函數,獲取所有的HTML代碼,然後向伺服器端發出非同步請求,把代碼發出去就是了。

❷ php 如何調用html文件,我的PHP文件是整個網頁的模板,中間想放個html文件,哪位可有好招

這個很簡單啊,用include_once()就可以,當然有很多種,例如:
include_once('./common.html');
common.html與你的php頁面在同一級的目錄下
不在同級就打入相應的路徑。

❸ PHP獲取遠程頁面html

file_get_contents()比file()慢?

如果抓取別人的頁面還是用file_get_contents()比較好~
file()取回的是數組
而file_get_contents()取回的是字元串

你是想取回個數組再把它們連起來方便?還是直接把他們取回來方便?

另外
file_get_contents(String,int)
有個可選參數設定讀取的長度

在PHP手冊中說:
file_get_contents() 函數是用來將文件的內容讀入到一個字元串中的首選方法。如果操作系統支持還會使用內存映射技術來增強性能。

❹ php讀取html指定部分

<?php
$re='/<li title=([^:]*):([^<]*)<\/li>/';
$x_goods='<li title="貨號">貨號:252-26-002252</li>
<li title="性別">性別:女</li>
<li title="款式">款式:手拿包</li>
<li title="背包方式">背包方式:手抓</li>
<li title="背包部位">背包部位:手部</li>
<li title="質地">質地:牛皮</li>
<li title="提拎部件">提拎部件:鎖鏈式提把</li>
<li title="內部結構">內部結構:拉鏈暗袋 夾層拉鏈袋
</li>
<li title="品牌">品牌:Tucano/義大利啄木鳥</li>
<li title="風格">風格:甜美淑女</li>
<li title="箱包外形">箱包外形:橫款方形</li>
<li title="顏色">顏色:白色</li>
<li title="有無夾層">有無夾層:有</li>
<li title="有無拉桿">有無拉桿:無</li>
<li title="有無手腕帶">有無手腕帶:有</li>
<li title="價格區間">價格區間:101-500元</li>
<li title="成色">成色:全新</li>
';
if(preg_match_all($re,$x_goods,$matches)){
print_r($matches[2]);
//$daya=explode(' ',trim($matches[5][0]));
}
exit;
?>

❺ 如何從php頁面跳轉到html頁面

如何從php頁面跳轉到html頁面
PHP頁面 首先要是在伺服器里運行,即不能像Html頁面一樣直接雙擊查看,而是要在瀏覽器里輸入相關地址才能訪問; 其次,PHP代碼被執行的文件,必須是以.php結尾,不能是在.html結尾; 再次,PHP代碼有起始標簽 <?php ?>標簽不能少,少了它就當Html代碼認了

❻ 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如何獲取需要登陸後才能看到的網頁HTML代碼

實際上是個模擬登陸的問題,需要寫個登陸模塊,解決兩個問題:
1,請求登陸並刷新的函數部分:

<?php
/*****************函數部分**************************/
/*獲取指定網頁的內容
$url為網頁地址
*/
function getcontent($url){
if($open=file($url)){
$count=count($open);
for($i=0;$i<$count;$i++)
{
$theget.=$open[$i];
}
}else{
die('請求過多,超時,請刷新');
}
return $theget;
}
?>
2,偷取程序部分,也分兩部分,
1),PHP與XML不同之處是需要特殊的調用才能支持COOKIE.或者記錄SessionID(後面有說明程序)

php代碼如下
<?PHP
//登陸並保存COOKIE
$f = fsockopen("www.url.net",80);
$cmd = <<<EOT
GET /test/login.php?name=test&password=test HTTP/1.0

EOT;
fputs($f,$cmd);
$result = '';
$cookie = '';
$location = '';
while($line = fgets($f))
{
$result .= $line;
//取得location跟setCookie頭HTTP頭信息
$tmp = explode(":",$line);
if($tmp[0]=="Set-Cookie")
$cookie .= $tmp[1];
if($tmp[0]=="Location")
$location = $tmp[1];
}
fclose($f);
2),獲取頁面
//下面訪問你要訪問的頁面(這部分也可以參考下面的核心常式)
$f = fsockopen("www.url.net",80);l
//下面的cookie就是發送前頁保存下的的cookie
$cmd = <<<EOT
GET /test/test.php HTTP/1.0
cookie:$cookie

EOT;
fputs($f,$cmd);
while($line = fgets($f))
{
echo $line;
}
fclose($f);
?>

核心常式就是fsockopen();
不妨再給段代碼你瞧瞧:
--------------------------------------------------------------------------------

function posttohost($url, $data)
{
$url = parse_url($url);
if (!$url) return "couldn't parse url";
if (!isset($url['port'])) { $url['port'] = ""; }
if (!isset($url['query'])) { $url['query'] = ""; }
$encoded = "";
while (list($k,$v) = each($data))
{
$encoded .= ($encoded ? "&" : "");
$encoded .= rawurlencode($k)."=".rawurlencode($v);
}
$fp = fsockopen($url['host'], $url['port'] ? $url['port'] : 80);
if (!$fp) return "Failed to open socket to $url[host]";
fputs($fp, sprintf("POST %s%s%s HTTP/1.0", $url['path'], $url['query'] ? "?" : "", $url['query']));
fputs($fp, "Host: $url[host]");
fputs($fp, "Content-type: application/x-www-form-urlencoded");
fputs($fp, "Content-length: " . strlen($encoded) . "");
fputs($fp, "Connection: close");
fputs($fp, "$encoded");
$line = fgets($fp,1024);
if (!eregi("^HTTP/1\\.. 200", $line)) return $line ;
$results = ""; $inheader = 1;
while(!feof($fp))
{
$line = fgets($fp,1024);
if ($inheader && ($line == "" || $line == "\r")) {
$inheader = 0;
}
elseif (!$inheader) {
$results .= $line;
}
}
fclose($fp);
return $results;
}
$data=array();
$data["msg"]="HELLO THIS IS TEST MSG";
$data["Type"]="TEXT";
echo posttohost("http://url/xxx", $data);

應該說明白了吧?
另外登陸部分還有一種簡單方法是把SessionID保存下來

源代碼:

<?php
/*
* 得到網頁內容
* 參數:$host [in] string
* 主機名稱(例如: www.url.com.cn)
* 參數:$method [in] string
* 提交方法:POST, GET, HEAD ... 並加上相應的參數( 具體語法參見 RFC1945,RFC2068 )
* 參數:$str [in] string
* 提交的內容
* 參數:$sessid [in] string
* PHP的SESSIONID
*
* @返回 網頁內容 string
*/
function GetWebContent($host, $method, $str, $sessid = '')
{
$ip = gethostbyname($host);
$fp = fsockopen($ip, 80);
if (!$fp) return;
fputs($fp, "$method\r\n");
fputs($fp, "Host: $host\r\n");
if (!empty($sessid))
{
fputs($fp, "Cookie: PHPSESSID=$sessid; path=/;\r\n");
}
if ( substr(trim($method),0, 4) == "POST")
{
fputs($fp, "Content-Length: ". strlen($str) . "\r\n"); // 別忘了指定長度
}
fputs($fp, "Content-Type: application/x-www-form-urlencoded\r\n\r\n");
if ( substr(trim($method),0, 4) == "POST")
{
fputs($fp, $str."\r\n");
}
while(!feof($fp))
{
$response .= fgets($fp, 1024);
}
$hlen = strpos($response,"\r\n\r\n"); // LINUX下是 "\n\n"
$header = substr($response, 0, $hlen);
$entity = substr($response, $hlen + 4);
if ( preg_match('/PHPSESSID=([0-9a-z]+);/i', $header, $matches))
{
$a['sessid'] = $matches[1];
}
if ( preg_match('/Location: ([0-9a-z\_\?\=\&\#\.]+)/i', $header, $matches))
{
$a['location'] = $matches[1];
}
$a['content'] = $entity;
fclose($fp);
return $a;
}

/* 構造用戶名,密碼字元串 */
$str = ("username=test&password=test");
$response = GetWebContent("localhost","POST /login.php HTTP/1.0", $str);
echo $response['location'].$response['content']."<br>";
echo $response['sessid']."<br>";
if ( preg_match('/error\.php/i',$response['location']))
{
echo "登陸失敗<br>";
} else {
echo "登陸成功<br>";
// 不可以訪問user.php,因為不帶sessid參數
$response = GetWebContent("localhost","GET /user.php HTTP/1.0", '', '');
echo $response['location']."<br>"; // 結果:error.php?errcode=2

// 可以訪問user.php
$response = GetWebContent("localhost","GET /user.php HTTP/1.0", '', $response['sessid']);
echo $response['location']."<br>"; // 結果:user.php
}
?>

熱點內容
家用電腦安裝伺服器內存 發布:2025-02-01 14:38:50 瀏覽:257
增量調制編解碼實驗報告 發布:2025-02-01 14:30:30 瀏覽:787
不良人2無敵傷害腳本 發布:2025-02-01 14:23:04 瀏覽:398
地圖flash源碼 發布:2025-02-01 14:13:33 瀏覽:957
家庭影院配置什麼樣的音響 發布:2025-02-01 14:04:33 瀏覽:545
蘋果手機存儲空間不能用怎麼回事 發布:2025-02-01 14:03:04 瀏覽:259
qq易語言盜號源碼 發布:2025-02-01 14:01:25 瀏覽:812
源神比較好的雲伺服器 發布:2025-02-01 13:55:27 瀏覽:208
黑蘋果idea編譯慢 發布:2025-02-01 13:45:30 瀏覽:552
c和linux 發布:2025-02-01 13:39:38 瀏覽:177