導航:首頁 > 編程語言 > 獲取useragentphp

獲取useragentphp

發布時間:2023-03-23 11:08:59

A. php獲取網頁源碼內容有哪些辦法

1、使用file_get_contents獲得網頁源代碼。這個方法最常用,只需要兩行代碼即可,非常簡單方孝春便。

2、使用fopen獲得網頁源代碼。這個方法用的人也不少,不過代碼有點多。

3、使用curl獲得網頁源代碼。使用curl獲得網頁源代碼的做法,往往是需要更高要求的人使用,例如當你需要在抓取網頁內容的同時,得到網頁header信息賣核,還有ENCODING編碼的使,USERAGENT的使用等等。

B. php 怎麼獲取內網IP

ip地址分 IPv4和IPV6兩個版本。

127.0.0.1或者192.168.0.1之類的,屬於IPV4的格式

127.0.0.1轉換為IPv6就是 ::1

IPv6具體的格式寫法,你搜索一下了解一下。

另外 ,服務端獲取ip地址,是相對的。比如你在區域網內建一個伺服器,在程序里獲取的ip地址,就是客戶端在區域網的ip ,一般是類似 192.168.0.101 之類的,大公司的話可能 10.x.x.x

區域網的ip地址,可以直接在網路適配器上看到

而在inter網,也就是大網路上,服務端獲取的ip地址,是你的連網設備獲取到的公網ip ,一般一個區域網內的主機,相對於網際網路上的伺服器,顯示的都是公網地址。

公網ip沒有在本機電腦上直接顯示。會顯示在路由或者連網設備上。也可以通過網路搜索 ip地址,會顯示你的公網ip

所以,假設一個區域網內,有兩台電腦同時訪問一個網上的伺服器,伺服器是沒辦法給兩台電腦區分開ip地址的,顯示的是一樣的。只是可能得到的設備信息不一樣(如:瀏覽器的useragent)

C. php 如何區分 useragent是不是偽造的

PHP通過內置全局變數$_SERVER['HTTP_USER_AGENT']來獲取用戶信息,包括瀏覽器信息,操作系統等;判斷是否是手機還是電腦終端訪問,只需判斷他的$_SERVER['HTTP_USER_AGENT']信息是否存在手機終端類型即可。示例如下:

D. php獲取數據為什麼curl獲取不完整

因為,PHP CURL庫默認1024位元組的長度不等待數據的返回,所以你那段代碼需增加一項配置:

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
給你一個更全面的封裝方法:

function req_curl($url, &$status = null, $options = array())
{
$res = '';
$options = array_merge(array(
'follow_local' => true,
'timeout' => 30,
'max_redirects' => 4,
'binary_transfer' => false,
'include_header' => false,
'no_body' => false,
'cookie_location' => dirname(__FILE__) . '/cookie',
'useragent' => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1',
'post' => array() ,
'referer' => null,
'ssl_verifypeer' => 0,
'ssl_verifyhost' => 0,
'headers' => array(
'Expect:'
) ,
'auth_name' => '',
'auth_pass' => '',
'session' => false
) , $options);
$options['url'] = $url;
$s = curl_init();
if (!$s) return false;
curl_setopt($s, CURLOPT_URL, $options['url']);
curl_setopt($s, CURLOPT_HTTPHEADER, $options['headers']);
curl_setopt($s, CURLOPT_SSL_VERIFYPEER, $options['ssl_verifypeer']);
curl_setopt($s, CURLOPT_SSL_VERIFYHOST, $options['ssl_verifyhost']);
curl_setopt($s, CURLOPT_TIMEOUT, $options['timeout']);
curl_setopt($s, CURLOPT_MAXREDIRS, $options['max_redirects']);
curl_setopt($s, CURLOPT_RETURNTRANSFER, true);
curl_setopt($s, CURLOPT_FOLLOWLOCATION, $options['follow_local']);
curl_setopt($s, CURLOPT_COOKIEJAR, $options['cookie_location']);
curl_setopt($s, CURLOPT_COOKIEFILE, $options['cookie_location']);
if (!empty($options['auth_name']) && is_string($options['auth_name']))
{
curl_setopt($s, CURLOPT_USERPWD, $options['auth_name'] . ':' . $options['auth_pass']);
}
if (!empty($options['post']))
{
curl_setopt($s, CURLOPT_POST, true);
curl_setopt($s, CURLOPT_POSTFIELDS, $options['post']);
//curl_setopt($s, CURLOPT_POSTFIELDS, array('username' => 'aeon', 'password' => '111111'));
}
if ($options['include_header'])
{
curl_setopt($s, CURLOPT_HEADER, true);
}
if ($options['no_body'])
{
curl_setopt($s, CURLOPT_NOBODY, true);
}
if ($options['session'])
{
curl_setopt($s, CURLOPT_COOKIESESSION, true);
curl_setopt($s, CURLOPT_COOKIE, $options['session']);
}
curl_setopt($s, CURLOPT_USERAGENT, $options['useragent']);
curl_setopt($s, CURLOPT_REFERER, $options['referer']);
$res = curl_exec($s);
$status = curl_getinfo($s, CURLINFO_HTTP_CODE);
curl_close($s);
return $res;
}

E. PHP判斷普通用戶或蜘蛛,調用不同代碼

定義一個函數 get_naps_bot()
如果是 BOT 則返回字元串, 如果不是 BOT 返回 false

function get_naps_bot()
{
$useragent = strtolower($_SERVER['HTTP_USER_AGENT']);
if (strpos($useragent, 'googlebot') !== false){
return 'Googlebot';
}

if (strpos($useragent, 'msnbot') !== false){
return 'MSNbot';
}

if (strpos($useragent, 'slurp') !== false){
return 'Yahoobot';
}

if (strpos($useragent, 'spider') !== false){
return 'Baispider';
}

if (strpos($useragent, 'sohu-search') !== false){
return 'Sohubot';
}

if (strpos($useragent, 'lycos') !== false){
return 'Lycos';
}

if (strpos($useragent, 'robozilla') !== false){
return 'Robozilla';
}
return false;
}

$botName = get_naps_bot();
if( empty($botName ) )
{
include( "11.php" );// 用戶訪問
}
else
{
include( "22.php" ); // 蜘蛛訪問
}

F. 如何用php獲取指定地址 的網頁源文件

$info=file_get_contents('https://..com/question/1513631908043601820.html?entry=&ishq=1');

//info就是網頁的源文件可以直接輸出也可以保存

echofile_put_contents('./test.html',$info)?'保存成功':'保存失敗';//保存

G. php獲取網頁源碼內容有哪些辦法

可以參考以下幾種方法:

方法一: 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還可以執行編譯後代碼,編譯可以達到加密和優化代碼運行,使代碼運行更快。

H. 如何用 php 抓取微信文章正文

可以嘗試使用DOM操作庫simple-html-dom.php,快速獲取HTML結構的內容:
<?php
require dirname(__FILE__).'/simple_html_dom.php';
$html = file_get_html('http://php.net/');
$articles = array();
foreach($html->find('article.newsentry') as $article) {
$item['time'] = trim($article->find('time', 0)->plaintext);
$item['title'] = trim($article->find('h2.newstitle', 0)->plaintext);
$item['content'] = trim($article->find('div.newscontent', 0)->plaintext);
$articles[] = $item;
}
print_r($articles);

可以把抓取到的內容寫入置於內存上的SQLite(/run/shm/php/crawler.db3),避免頻繁的磁碟IO.

I. 如何在php中獲取curl請求的請求頭信息及相應頭信息

oCurl=curl_init();

//設置請求頭

$header[]="Content-type:application/x-www-form-urlencoded";

$user_agent="Mozilla/5.0(WindowsNT6.1)AppleWebKit/537.36(KHTML,likeGecko)Chrome/33.0.1750.146Safari/537.36";

curl_setopt($oCurl,CURLOPT_URL,$sUrl);

curl_setopt($oCurl,CURLOPT_HTTPHEADER,$header);

//返回response_header,該選項非常重要,如果不為true,只會獲得響應的正文

curl_setopt($oCurl,CURLOPT_HEADER,true);

//是否不需要響應的正文,為了節省帶寬及時間,在只需要響應頭的情況下可以不要正文

curl_setopt($oCurl,CURLOPT_NOBODY,true);

//使用上面定義的uacurl_setopt($oCurl,CURLOPT_USERAGENT,$user_agent);curl_setopt($oCurl,CURLOPT_RETURNTRANSFER,1);

//不用POST方式請求,意思就是通過GET請求

curl_setopt($oCurl,CURLOPT_POST,false);$sContent=curl_exec($oCurl);

//獲得響應結果里的:頭大小

$headerSize=curl_getinfo($oCurl,CURLINFO_HEADER_SIZE);

//根據頭大小去獲取頭信息內容

$header=substr($sContent,0,$headerSize);

curl_close($oCurl);

J. php 獲取網頁頭部信息和網頁和網頁源代碼查看

<?php
/**
* http下載類庫
*/
class Httplib{
// 目標網站無法打開時返回的錯誤代碼
var $_ERROR_CONNECT_FAILURE = 600;
// 自定義 UserAgent 字元串
var $_SEND_USER_AGENT = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; LazyCMS.net::DownLoader)';
var $_url,$_method,$_timeout;
var $_scheme,$_host,$_port,$_path,$_query,$_referer;
var $_header;
var $_response;
/**
* 兼容PHP5模式
*
* @param 同下面的參數
*/
function __construct($url=null,$method='GET',$timeout=60){
@set_time_limit(0);
if (!empty($url)) {
$this->connect($url,$method,$timeout);
}
return $this;
}
/**
* 初始化對象
*
* @param string $url
* @param string $method
* @param int $timeout
* @return object
*/
function Httplib($url=null,$method='GET',$timeout=60){
return $this->__construct($url,$method,$timeout);
}
/**
* 改變連接url
*
* @param string $url
* @param string $method
* @param int $timeout
* @return object
*/
function connect($url=null,$method='GET',$timeout=60){
$this->_header = null;
$this->_response = null;
$this->_url = $url;
$this->_method = strtoupper(empty($method) ? 'GET' : $method);
$this->_timeout = empty($timeout) ? 30 : $timeout;
if (!empty($url)) {
$this->_parseURL($url);
}
return $this;
}
/**
* 發送請求
*
* @param array $params
* @return bool
*/
function send($params=array()) {
$header = null; $response = null; $QueryStr = null;
if (!empty($params)) { $this->_method = 'POST'; }
if (function_exists('fsockopen')) {
$fp = @fsockopen($this->_host,$this->_port,$errno,$errstr,$this->_timeout);
if (!$fp) { return false; }
$_port = ((int)$this->_port!==80) ? ':'.$this->_port : null;
$SendStr = "{$this->_method} {$this->_path}{$this->_query} HTTP/1.0\r\n";
$SendStr.= "Host:{$this->_host}{$_port}\r\n";
$SendStr.= "Accept: */*\r\n";
$SendStr.= "Referer:{$this->_referer}\r\n";
$SendStr.= "User-Agent: ".$this->_SEND_USER_AGENT."\r\n";
$SendStr.= "Pragma: no-cache\r\n";
$SendStr.= "Cache-Control: no-cache\r\n";
//如果是POST方法,分析參數
if ($this->_method=='POST') {
//判斷參數是否是數組,循環出查詢字元串
if (is_array($params)) {
$QueryStr = http_build_query($params);
} else {
$QueryStr = $params;
}
$length = strlen($QueryStr);
$SendStr.= "Content-Type: application/x-www-form-urlencoded\r\n";
$SendStr.= "Content-Length: {$length}\r\n";
}
$SendStr.= "Connection: Close\r\n\r\n";
if(strlen($QueryStr) > 0){
$SendStr.= $QueryStr."\r\n";
}
fputs($fp,$SendStr);
// 讀取 header
do{ $header.= fread($fp,1); } while (!preg_match("/\r\n\r\n$/",$header));
// 遇到跳轉,執行跟蹤跳轉
if ($this->_redirect($header)) { return true; }
// 讀取內容
while(!feof($fp)) {
$response.= fread($fp,4096);
}
fclose($fp);
} elseif (function_exists('curl_exec')) {
$ch = curl_init($this->_url);
curl_setopt_array($ch,array(
CURLOPT_TIMEOUT => $this->_timeout,
CURLOPT_HEADER => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_USERAGENT => $this->_SEND_USER_AGENT,
CURLOPT_REFERER => $this->_referer,
));
if ($this->_method=='GET') {
curl_setopt($ch,CURLOPT_HTTPGET,true);
} else {
if (is_array($params)) {
$QueryStr = http_build_query($params);
} else {
$QueryStr = $params;
}
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_POSTFIELDS,$QueryStr);
}
$fp = curl_exec($ch);
curl_close($ch);
if (!$fp) { return false; }
$i = 0; $length = strlen($fp);
// 讀取 header
do{ $header.= substr($fp,$i,1); $i++; } while (!preg_match("/\r\n\r\n$/",$header));
// 遇到跳轉,執行跟蹤跳轉
if ($this->_redirect($header)) { return true; }
// 讀取內容
do {
$response.= substr($fp,$i,4096);
$i = $i + 4096;
} while ($length>=$i);
unset($fp,$length,$i);
}
$this->_header = $header;
$this->_response = $response;
return true;
}
/**
* 跟蹤跳轉
*
* @param string $header
* @return bool
*/
function _redirect($header){
if (in_array($this->status($header),array(301,302))) {
if(preg_match("/Location\:(.+)\r\n/i",$header,$regs)){
$this->connect(trim($regs[1]),$this->_method,$this->_timeout);
$this->send();
return true;
}
} else {
return false;
}
}
/**
* 取得請求的header
*
* @return string
*/
function header(){
return $this->_header;
}
/**
* 請求返回的html
*
* @return string
*/
function response(){
return $this->_response;
}
/**
* 返回狀態
*
* @param string $header
* @return int
*/
function status($header=null){
if (empty($header)) {
$header = $this->_header;
}
if(preg_match("/(.+) (\d+) (.+)\r\n/i",$header,$status)){
return $status[2];
} else {
return $this->_ERROR_CONNECT_FAILURE;
}
}
/**
* 解析url
*
* @param string $url
*/
function _parseURL($url){
$aUrl = parse_url($url);
$aUrl['query'] = isset($aUrl['query']) ? $aUrl['query'] : null;
$scheme = isset($_SERVER['HTTPS']) ? $_SERVER['HTTPS'] : null;
$this->_scheme = ($scheme=='off' || empty($scheme)) ? 'http' : 'https';
$this->_host = isset($aUrl['host']) ? $aUrl['host'] : null;
$this->_port = empty($aUrl['port']) ? 80 : (int)$aUrl['host'];
$this->_path = empty($aUrl['path']) ? '/' : (string)$aUrl['path'];
$this->_query = strlen($aUrl['query']) > 0 ? '?'.$aUrl['query'] : null;
$this->_referer = $this->_scheme.'://'.$aUrl['host'];
}
}

$http = new Httplib('http://www..com');
$http->send();
$body = $http->response();
echo $body;

閱讀全文

與獲取useragentphp相關的資料

熱點內容
php讀取資料庫中文亂碼 瀏覽:774
小說的藝術pdf 瀏覽:797
linux硬體命令 瀏覽:857
空調製冷壓縮機用來打氣嗎 瀏覽:491
加密文件屬性能刪除嗎 瀏覽:60
綠本沒有抵押章還完還需要解壓嗎 瀏覽:92
編譯原理與技術第二版學習指導 瀏覽:352
退出京東購物哪個app好 瀏覽:257
單片機可調直流穩壓 瀏覽:201
程序員文檔結構 瀏覽:834
娛樂模式命令 瀏覽:512
宏傑文件加密軟體收費了 瀏覽:513
php大數據處理教程 瀏覽:227
文件夾的順序按結尾一二三四排列 瀏覽:837
剪切命令的快捷鍵是 瀏覽:494
安卓導航背光不夠怎麼辦 瀏覽:514
光遇用的是什麼伺服器 瀏覽:86
wcf服務編程第三版pdf 瀏覽:308
匈牙利演算法java 瀏覽:381
服務區提供什麼伺服器地址 瀏覽:344