PHP模拟浏览器请求(解决谷歌广告不显示的问题)
更新:2023-05-08 09:53:32
人气:1180
来源:本站原创
A+
问题来源:为了一点点收入挂了谷歌广告联盟的广告Google AdSense,但是新发布文章会出现很长一段时间都无法加载广告的情况,使得广告展现率很低也很影响收入。经过查询大概情况是谷歌要在页面被访问后才能分析出来适合匹配的广告,如果这样的话每次第一个访问者来访问都会造成广告不能显示,我就想着能不能在自己博客发布完文章后自己触发程序将这一批的文章都模拟浏览器请求访问一次并停留一段时间。
考虑两个方案:1、用curl模拟访问 2、用框架页动态加载,这次先用curl测试一下效果,相关代码记录如下:
PHP模拟浏览器请求(解决谷歌广告不显示的问题)
<?
class Request
{
private $curl;
private $query = '';
private $user_agent = 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Mobile Safari/537.36';
public function __construct()
{
$this->curl = curl_init(); // 初始化
}
public function request($url, $data = [], $type = 'GET')
{
$ret = '';
switch ($type) {
case 'GET';
$ret = $this->get($url, $data);
break;
case 'POST';
$ret = $this->post($url, $data);
break;
}
return $ret;
}
// 模拟浏览器get请求
public function get($url, $data)
{
if ($data) {
foreach ($data as $key => $value) {
$this->query .= $key . '=' . $value;
}
$url .= '?' . $this->query;
}
curl_setopt($this->curl, CURLOPT_PROXY, $GLOBALS ['proxy']);//代理服务器地址
curl_setopt($this->curl, CURLOPT_URL, $url); // 要访问的地址
curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
curl_setopt($this->curl, CURLOPT_SSL_VERIFYHOST, 2); // 从证书中检查SSL加密算法是否存在
curl_setopt($this->curl, CURLOPT_USERAGENT, $this->user_agent); // 模拟用户使用的浏览器
@curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
curl_setopt($this->curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
curl_setopt($this->curl, CURLOPT_HTTPGET, 1); // 发送一个常规的Post请求
curl_setopt($this->curl, CURLOPT_COOKIEFILE, $GLOBALS ['cookie_file']); // 读取上面所储存的Cookie信息
curl_setopt($this->curl, CURLOPT_TIMEOUT, 120); // 设置超时限制防止死循环
curl_setopt($this->curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
$tmpInfo = curl_exec($this->curl); // 执行操作
return $tmpInfo;
}
// 模拟浏览器post请求
public function post($url, $data)
{
//代理服务器地址
curl_setopt($this->curl, CURLOPT_PROXY, $GLOBALS ['proxy']);
curl_setopt($this->curl, CURLOPT_URL, $url); // 要访问的地址
curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
curl_setopt($this->curl, CURLOPT_SSL_VERIFYHOST, 2); // 从证书中检查SSL加密算法是否存在
curl_setopt($this->curl, CURLOPT_USERAGENT, $this->user_agent); // 模拟用户使用的浏览器
@curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
curl_setopt($this->curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
curl_setopt($this->curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包
curl_setopt($this->curl, CURLOPT_COOKIEFILE, $GLOBALS ['cookie_file']); // 读取上面所储存的Cookie信息
curl_setopt($this->curl, CURLOPT_TIMEOUT, 120); // 设置超时限制防止死循环
curl_setopt($this->curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
$tmpInfo = curl_exec($this->curl); // 执行操作
return $tmpInfo;
}
public function __destruct()
{
//关闭URL请求
curl_close($this->curl);
}
}
$request = new Request();
$request->get('https://www.mdaima.com/it/10175.html','');
exit;
?>
推荐的文章
随手记
- ● 自制(IP或域名)可信任的SSL证书,适用360、chrome等浏览器
- ● windows系统下php无法使用curl怎么办?
- ● 绿联UGREENKVM切换器(分屏器)快捷键丢失解决办法
- ● 统信UOS开机指定网址全屏启动自带浏览器以及屏蔽ALT+F4关闭
- ● xshellSSH连接Linux服务器防止超时退出
- ● php8开启OpenSSL扩展库报错disabledinstallext
- ● 统信系统linux安装php时的报错libxml-2.0>=2.7.6
- ● tidb关闭sql_mode=ONLY_FULL_GROUP_BY模式
- ● windows10如何开机自动运行bat文件
- ● Win10Mysql8初始密码丢失,初始化又不显示密码
PHP经验分享
- ● PHP批量对TCP服务端指定多个IP非阻塞检查在线状态
- ● python实现TCP服务端持续接收关机、重启指令并输出结果【系列三】
- ● PHP给TCP服务端发送指令【系列二】
- ● PHP判断TCP服务端是否在线【系列一】
- ● PHP判断远程文件是否存在
- ● LINUX下用PHP获取CPU型号、内存占用、硬盘占用等信息代码
- ● PHP代码用UDP方式远程唤醒电脑让计算机开机
- ● apache下php生成验证码图片不能显示
- ● PHP使用AES加密解密示例(无偏移)
- ● Pluginmysql_native_passwordreported:''mysql_native_password'isdeprecate问题