如何用php伪造浏览器ua?

如何用php伪造浏览器ua?,第1张

file_get_contents 和 curl 这俩强悍的函数,在远程抓取时候相当有用处。不过一些网站会根据来访ip是否携带user_agent来判断是正常的浏览器客户端还是机器。所以,我们的任务就是给他们伪造user_agent。

file_get_contents伪造user_agent 方法如下:

ini_set('user_agent','Mozilla/4.0 (compatibleMSIE 6.0Windows NT 5.1SV1.NET CLR 2.0.50727http://www.baidu.com)')

curl伪造user_agent的方法:

curl_setopt($c, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatibleMSIE 6.0Windows NT 5.1SV1.NET CLR 2.0.50727http://www.baidu.com)')

附带上curl的完整函数方法:

function curl_get_file_contents($URL)

{

$c = curl_init()

curl_setopt($c, CURLOPT_RETURNTRANSFER, 1)

//curl_setopt($c, CURLOPT_HEADER, 1)//输出远程服务器的header信息

curl_setopt($c, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatibleMSIE 6.0Windows NT 5.1SV1.NET CLR 2.0.50727http://www.baidu.com)')

curl_setopt($c, CURLOPT_URL, $URL)

$contents = curl_exec($c)

curl_close($c)

if ($contents) {return $contents}

else {return FALSE}

}

这样就可以抓取了,user_agent是可以自己修改的。

在curl里可以设置UA

<?php

//client

$ch = curl_init()

curl_setopt_array($ch, 

array(

CURLOPT_URL => 'http://localhost/ua.php',

CURLOPT_USERAGENT => "YeRenChai_v1.0",

CURLOPT_RETURNTRANSFER => True,

CURLOPT_FOLLOWLOCATION => True,

)

)

$response = curl_exec($ch)

if(!$response) exit(curl_error($ch))

var_dump($response)

?> <?php //server

    echo $_SERVER['HTTP_USER_AGENT']

?>


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/tougao/11313844.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-15
下一篇 2023-05-15

发表评论

登录后才能评论

评论列表(0条)

保存