php获取微信公众号评论留言数据

王三去 2021-06-16 22:40:17
认证信息:www.sanqu.cc官方账号

官方文档地址:查看指定文章的评论数据(新增接口)

https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1494572718_WzHIY

使用说明:这是我平时用于留言随机抽奖的部分代码,主要可以将评论的openid和留言内容等信息写入数据,供后续抽奖及下发红包使用。

1.获取access token

这部分不做讲解

2.调用接口

 $msg_data_id=$_GET['id'];
 
   $page=$_GET['page'];
  $page3=$page+1;
  
  if($page==1){
      
      $page2='0';
      
  }else{
      
      
      $page2=$page*1;
  }
  
$data = '{
"msg_data_id":"'.$msg_data_id.'",
"index":"0",
"begin":"'.$page2.'",
"count":"1",
"type":"0"
}';
$url = "https://api.weixin.qq.com/cgi-bin/comment/list?access_token=$access_token";
$result = https_request($url, $data);

//var_dump($result);

//echo $result;

$obj=json_decode($result);

  $data=$obj->comment;
  $i=0;
   foreach ( $data as $unit )
   {
       $i++;
       $arr[$i]['user_comment_id']=$unit->user_comment_id;
       $arr[$i]['content']=$unit->content;
       $arr[$i]['openid']=$unit->openid;
       
     //  echo $arr[$i]['openid'].'
';
       
                      
                       

        


       

  }


function https_request($url, $data = null)
{
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
    if (!empty($data)){
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    }
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($curl);
    curl_close($curl);
    return $output;
}