如何用php爬取微信公众号的文章
有如下方法
1、通过搜狗搜索,这里只能爬取最新发布的文章
2、登录自己的微信公众号,然后在图文消息中选择超链接
![](https://static.yuanchengzhushou.cn/gzh.png)
然后按f12查看相关的微信api调用
![](https://static.yuanchengzhushou.cn/gzh2.png)
开始编码
```
public function test(){
$ch = curl_init();
$begin = input('begin')*5;
$requesturl = "https://mp.weixin.qq.com/cgi-bin/appmsg?action=list_ex&begin=$begin&count=5&fakeid=Mzk0MTE2MDU0Ng==&type=9&query=&token=892078507&lang=zh_CN&f=json&ajax=1";
curl_setopt($ch, CURLOPT_URL, $requesturl);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//$cookie = 'PHPSESSID=7hrgnng8c8f2h0c288uuol2dna; loginToken=1';
//$cookie = 'PHPSESSID=tn5s5b97sbfov1amtrdt290sbp; loginToken=147082ba99e0a0c1d8d542908382d0e0';
$cookie = 'sig_login=h01d512e0896f0c4e7a2131b98174c73e6d7a120df60246225dee7817906e39add3014a514c41c55f4f; ptui_loginuin=272***425; RK=feJMz1nqOC; ptcz=71c3a1d9cdf5b3c0410ae728566ba6d7483c7fdfa2b99c482fd6c2c5acc22520; Qs_lvt_323937=1640152449; Qs_pv_323937=4584984440836307500; rewardsn=; wxtokenkey=777; tvfe_boss_uuid=a467528e20f195ec; pgv_pvid=6462174230; pgv_info=ssid=s1852355569; vversion_name=8.2.95; video_omgid=f3c049416a123cf3; gamerqqcomrouteLine=index; tokenParams=%3Fnav%3D1%26ichannel%3Dtxsppc0Ftxsp1; ua_id=NtGfaFySWQGHF1AOAAAAAGdPByvtWfh10KZGTxelZxo=; pac_uid=0_a88f5467c61b1; uuid=84e4e920184589dbd4015478f0c01bcd; rand_info=CAESIIUQflxcOfJmAFQfWEdO7pSKMGCnFVFef7F5ukm0eYZO; slave_bizuin=2394190652; data_bizuin=2394190652; bizuin=2394190652; data_ticket=mbsWhKhaM4Rw6nYnSPe1BubyaQgcmljNkXNlKDBmuAmhZgPsXFmzptrBsz7t8hfl; slave_sid=MlJHbGVxOEtxWEZpUk1PaDZfeDlmVVRFcWk5dENPM2N4SjZHS1JyS1ZzSTNXcWdXOVRhUHRYSkp1SmNHZWlGdHU3YzFVV0xMODZOd041eTFieWYzZTRPN250Qmk5VWVOVEo0TXFDVzdEMTc5Z21QSGhEUXdTeExtU0s4ZVlZTmVkTlJSazdTTmUxWVV0cWVH; slave_user=gh_3e2d1dcfd06a; xid=821994f6405fe5601be41391f3f42a05; mm_lang=zh_CN; wxuin=1320819460; devicetype=Windows10x64; version=6304051b; lang=zh_CN; pass_ticket=F3mZlT5sKbrRWKsziwuUMRuTlLD64agDHsTVrNNA9e0n1wcjwxtbdPjHKS3fu3Nh; appmsg_token=1145_U%2BMySVgzeqT%2BwoGNmYfRPGFcB5WtCAb7FOYVglQxFazz098GD0VUh-uPGxh5OInP5pu1BzYjviFARmv9; wap_sid2=CIS26PUEEooBeV9ITXd4N2ZrQm5sYTBlZmZQV0xfMW5nQncyMFdTelpWMkwwM3lsU29BNnRad0loNng1LWk1U1dvajRhZ3B6Vks0SXdwendnUUdxQkZVMi1BaFZ0aldlUUVwcW1hRnJTdGNNeDdrYkNBSldoNkZHNVo5cWVUeGpfTXdpTHJKSUdVWlYxWVNBQUF+MN+8lY4GOA1AAQ==; _qpsvr_localtk=0.2424666821109296';
curl_setopt($ch, CURLOPT_COOKIE, $cookie); //使用上面获取的cookies
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
$data=curl_exec($ch);
$data = json_decode($data,true);
echo "
"; foreach($data['app_msg_list'] as $k=>$v){ $list[$k]['title'] = $v['title']; } print_r($list); echo "
";
error_log(print_r($data,1),3,"/tmp/1.txt");
curl_close($ch);
}
```
效果如下
```
Array
(
[0] => Array
(
[title] => 仅剩最后半天,错过要等明年了!
)
[1] => Array
(
[title] => PHP组团活动来了!最后三天报名时间
)
[2] => Array
(
[title] => 为小白打造的,仿php中文网APP移动端开发(TP6+Vue3+阿里UI)
)
[3] => Array
(
[title] => php中文网APP(iOS+安卓)全新上线!
)
[4] => Array
(
[title] => 开课了!php程序员就业班3个月速成
)
[5] => Array
(
[title] => 开课了,php小白到大牛3个月速成
)
)
```