PHP 实现新浪微博自动评论及爬取微博id

public function jiaoben(){
		$code = $this->request->param('code');
		$access_token = session('access');
		echo $access_token;
		if($code && !$access_token){
			$ret = $this->getcode($code);
			$arr = json_decode($ret,true);
			if($arr['expires_in'] == 3600){
				session('access',$arr['access_token']);
			}
		}
        $url_token = "https://api.weibo.com/oauth2/access_token";//获取token
        $url_code = "https://api.weibo.com/oauth2/authorize";//获取code
        $code_data = "client_id=&response_type=code&redirect_uri=https://ceshi.yunyihw.com";//code数据
        $postdata = "client_id=&client_secret=&grant_type=authorization_code&code=&redirect_uri=https://ceshi.yunyihw.com";//token数据
        /* $url = 'https://api.weibo.com/2/comments/create.json';//评论url */
        /* $pp = "access_token=2.00DxpcuF0ah4yQe504a76ee8YSHSdC&comment=".urlencode('你好啊,很高兴见到你')."&id=4303785897751981";//评论数据 */
        $data_url = 'https://api.weibo.com/2/search/topics.json';
		$pp = "access_token=2.00DxpcuF0ah4yQe504a76ee8YSHSdC&q=".urlencode('你好啊,很高兴见到你')."&count=30";//数据
        /* $ret = Post($pp,$data_url); */
		return $this->fetch('jiaoben');
    }
	public function getcode($code){
		$url_token = "https://api.weibo.com/oauth2/access_token";//获取token
		$postdata = "client_id=&client_secret=&grant_type=authorization_code&code=".$code."&redirect_uri=https://ceshi.yunyihw.com/Hospital/jiaoben.html";
		$ret = Post($postdata,$url_token);
		return $ret;
	}
	public function pinglun(){
		$mids = $this->request->param('mids');
		$contents = $this->request->param('content');
		$content = urlencode($contents);
		$access_token = session('access');
		$midss = array_filter(explode(',',$mids));
		if($midss){
			foreach($midss as $k=>$v){
				$mid = $v;
				$pp = "access_token=".$access_token."&comment=".$content."&id=".$mid;//评论数据
				$url = 'https://api.weibo.com/2/comments/create.json';//评论url
		 		$ret = Post($pp,$url);
				$c = $k+1;
				echo "完成".$c."个";
				echo "</br>";
			}
		}
		/* echo "<pre>"; */
		/* print_r($mid);die; */
		// $pp = "access_token=".$access_token."&comment=".$content."&id=".$mid;//评论数据
		// $url = 'https://api.weibo.com/2/comments/create.json';//评论url
		// $ret = Post($pp,$data_url);
	}

具体步骤:授权拿到code-》用code换取access_token-》实现功能

微博开放平台http://open.weibo.com/wiki/2/search/topics

爬虫框架文档 https://www.querylist.cc/docs/guide/v4/installation       

爬虫框架代码 https://pan.baidu.com/s/1QPMUalL5HcVJ4L7zUY0LMg

爬虫代码

<?php

require 'vendor/autoload.php';

use QL\QueryList;
$url = 'https://s.weibo.com/weibo?q=%E7%9C%8B%E7%9C%8B%E9%80%9F%E5%BA%A6%E6%89%A9%E4%BB%98%E5%86%9B%E5%86%9B%E5%86%9B%E5%86%9B&typeall=1&suball=1&timescope=custom:2018-11-07:2018-11-09&Refer=SWeibo_box';
$data = QueryList::get($url)->find('div')->attrs('mid');
$arr = array_filter($data->all());
$cc = implode(',',$arr);
echo "<pre>";
print_r($cc);

 

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一捧

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值