服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - PHP教程 - PHP基于curl实现模拟微信浏览器打开微信链接的方法示例

PHP基于curl实现模拟微信浏览器打开微信链接的方法示例

2019-06-14 10:28无颠服务器之家 PHP教程

这篇文章主要介绍了PHP基于curl实现模拟微信浏览器打开微信链接的方法,结合实例形式分析了php使用curl通过设置HTTP_USER_AGENT实现模拟微信浏览器相关操作技巧,需要的朋友可以参考下

本文实例讲述了PHP基于curl实现模拟微信浏览器打开微信链接的方法。分享给大家供大家参考,具体如下:

网络上没有可以直接打开微信的浏览器 但是我们可以模拟浏览器

微信浏览器的HTTP_USER_AGENT

在iPhone下,返回

Mozilla/5.0 (iPhone; CPU iPhone OS 5_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Mobile/9B176 MicroMessenger/4.3.2

在Android下,返回

Mozilla/5.0 (Linux; U; Android 2.3.6; zh-cn; GT-S5660 Build/GINGERBREAD) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1 MicroMessenger/4.5.255

不难发现微信浏览器为 MicroMessenger ,并且有版本号,也可以判断手机类型为iPhone还是Android

php 模拟 微信浏览器 使用如下:

get 方式

function get($url, $referer, $cookie) {
  $header = array();
  $header[] = 'Accept: image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, */*';
  $header[] = 'Connection: Keep-Alive';
  $header[] = 'Accept-Language: zh-cn';
  $header[] = 'Cache-Control: no-cache';
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_HEADER, 1);
  curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (iPhone; CPU iPhone OS 5_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Mobile/9B176 MicroMessenger/4.3.2');
  curl_setopt($ch, CURLOPT_REFERER, $referer);
  curl_setopt($ch, CURLOPT_COOKIE, $cookie);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_TIMEOUT, 10);
  $result = curl_exec($ch);
  curl_close($ch);
  return $result;
}
echo get('http://wxt.wedoor.com/wxtAction.do?method=showyl&id=f20c503cac9349308c3a87ecbae5908c&scene=1','','');

post方式

function post($url, $data, $referer, $cookie) {
  $header = array();
  $header[] = 'Accept: image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, */*';
  $header[] = 'Connection: Keep-Alive';
  $header[] = 'Accept-Language: zh-cn';
  $header[] = 'Cache-Control: no-cache';
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_HEADER, 1);
  curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Linux; U; Android 2.3.6; zh-cn; GT-S5660 Build/GINGERBREAD) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1 MicroMessenger/4.5.255');
  curl_setopt($ch, CURLOPT_REFERER, $referer);
  curl_setopt($ch, CURLOPT_COOKIE, $cookie);
  curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_TIMEOUT, 10);
  $result = curl_exec($ch);
  curl_close($ch);
  return $result;
}
echo post('http://wxt.wedoor.com/wxtAction.do?method=showyl&id=f20c503cac9349308c3a87ecbae5908c&scene=1','','','');

如此这般就可以了

如果要做盗链

if(strpos($_SERVER["HTTP_USER_AGENT"],"MicroMessenger"))
  echo "Welcome to wechat word";
else
  echo "http/1.1 401 Unauthorized";

希望本文所述对大家PHP程序设计有所帮助。

延伸 · 阅读

精彩推荐