php獲取url函數
⑴ php取頁面所有的URL怎麼實現
function get_all_url($code){ preg_match_all(『/"\' ]+)["|\']?\s*[^>]*>([^>]+)<\/a>/i』,$code,$arr); return array(『name』=>$arr[2],』url』=>$arr[1]);}
⑵ php打開URL的幾種方法
PHP中打開URL地址的幾種方法總結,這里的函數主要用於小偷採集等函數。
1:用file_get_contents
以get方式獲取內容
復制代碼代碼如下:
<?php
$url='http://www..com/';
$html=file_get_contents($url);
//print_r($http_response_header);
ec($html);
printhr();
printarr($http_response_header);
printhr();
?>
示例代碼2:用fopen打開url,
以get方式獲取內容
復制代碼代碼如下:
<?
$fp=fopen($url,'r');
printarr(stream_get_meta_data($fp));
printhr();
while(!feof($fp)){
$result.=fgets($fp,1024);
}
echo"urlbody:$result";
printhr();
fclose($fp);
?>
示例代碼3:用file_get_contents函數,以post方式獲取url
復制代碼代碼如下:
<?php
$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
),
);
$context=
stream_context_create($opts);
$html=
file_get_contents('http://localhost/e/admin/test.html',false,$context);
echo$html;
?>
示例代碼4:用fsockopen函數打開url,以get方式獲取完整的數據,包括header和body
復制代碼代碼如下:
<?
functionget_url
($url,$cookie=false){
$url=parse_url($url);
$query=
$url[path]."?".$url[query];
ec("Query:".$query);
$fp=fsockopen(
$url[host],$url[port]?$url[port]:80,$errno,$errstr,30);
if(!$fp){
returnfalse;
}else{
$request="GET$queryHTTP/1.1";
$request.="Host:$url[host]";
$request.="Connection:Close";
if($cookie)$request.="Cookie:$cookie ";
$request.="";
fwrite($fp,$request);
while(!@feof($fp)){
$result.=@fgets($fp,
1024);
}
fclose($fp);
return$result;
}
}
//獲取url的html部分,去掉header
functionGetUrlHTML($url,$cookie=false){
$rowdata=get_url($url,$cookie);
if($rowdata)
{
$body=
stristr($rowdata,"");
$body=substr($body,4,strlen($body));
return$body;
}
returnfalse;
}
?>
⑶ php獲取url參數
1、在當前網頁echo出變數$_SERVER['HTTP_HOST']即可獲取域名或主機地址。
⑷ php如何獲取當前頁面url路徑
function curPageURL() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on")
{
$pageURL .= "s";
}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80")
{
$pageURL .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] .
$_SERVER["REQUEST_URI"];
}
else
{
$pageURL .= $_SERVER["SERVER_NAME"] .
$_SERVER["REQUEST_URI"];
}
return $pageURL;}?>
(4)php獲取url函數擴展閱讀:
獲取域名或主機地址 :echo $_SERVER['HTTP_HOST'].""; #localhost
獲取網頁地址:echo $_SERVER['PHP_SELF'].""; #/blog/testurl.php
3.獲取網址參數:echo $_SERVER["QUERY_STRING"].""; #id=5
4.獲取用戶代理:echo $_SERVER['HTTP_REFERER']."";
⑸ php語言獲得並判斷url
function isInString4($haystack, $needle) {
return false !== strpos($haystack, $needle);
}
var_mp(isInString1($haystack, $needle));
....
if(strpos($a, $b) !== ture){
執行語句1
}else{
執行語句2
}
⑹ 如何通過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怎樣獲得鏈接來源url
使用PHP編寫程序的時候,想要獲取當前頁面的URL,可以了用函數來實現;參考方法如下:php//說明:獲取完整URLfunctioncurPageURL(){$pageURL='http';if($_...