微信自动生成数据与发布

 不多说了,直接上源码,能实现登录,生成图文数据,且发布此图文数据

<?php
/*
使用方法:
 $arr = array(
    'account' => '公众平台帐号',
    'password' => '密码'
);
$w = new Weixin($arr);
$w->getAllUserInfo();//获取用户信息
$w->sendMessage('群发内容'); //群发给所有用户
$w->sendMessage('群发内容',$userId); //群发给特定用户
*/
class WeixinSend {
    public $userFakeid;//所有粉丝的fakeid
    private $_account;//用户名
    private $_password;//密码
    private $url;//请求的网址
    private $send_data;//提交的数据
    private $getHeader = 0;//是否显示Header信息
    private $token;//公共帐号TOKEN
    private $host = 'mp.weixin.qq.com';//主机
    private $origin = 'https://mp.weixin.qq.com';
    private $referer;//引用地址
    private $cookie;
    private $pageSize = 100000;//每页用户数(用于读取所有用户)
    private $userAgent = 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0';
     
     
    public function __construct($options){
        $this->_account = isset($options['account'])?$options['account']:'';
        $this->_password = isset($options['password'])?$options['password']:'';
        $this->login();
    }
     
    //登录
    private function login(){
        $url = 'https://mp.weixin.qq.com/cgi-bin/login?lang=zh_CN';
        $this->send_data = array(
            'username' => $this->_account,
            'pwd' => md5($this->_password),
            'f' => 'json'
        );
        $this->referer = "https://mp.weixin.qq.com/";
        $this->getHeader = 1;
         
        $result = explode("\n",$this->curlPost($url));
 
        $base_resp=json_decode($result[count($result)-1]);//最后一条返回信息
 
        $return_code    =   $base_resp->base_resp->ret;
        $error_msg      =   $base_resp->base_resp->err_msg;
        if($return_code!==0){
            die(json_encode(array('status'=>1,'errCode'=>$ret,'msg'=>$error_msg)));    
        }
        $redirect_url   =   $base_resp->redirect_url;//跳转路径
 
        if(preg_match('/token=([\d]+)/i', $redirect_url,$match)){//获取cookie
            $this->token=$match[1];
        }
 
        foreach($result as $row){
            if(preg_match('/Set-Cookie:[\s*]([^=]+)=([^;]+)/i', $row,$match)){//获取cookie
                $this->cookie .=$match[1].'='.$match[2].'; ';
            }
 
        }
 
    }
 
 
    //暂时只支持每天发送多图文资讯
    public function createMore(){
        $url = 'https://mp.weixin.qq.com/cgi-bin/operate_appmsg';
        $this->referer = "https://mp.weixin.qq.com/cgi-bin/appmsg?t=media/appmsg_edit&action=edit&lang=zh_CN&token=".$this->token."&type=10&appmsgid=201190706&isMul=1";
 
        $random_str="0.".mt_rand(1000000000000000,9999999999999999);
        $datas = array('AppMsgId' => "201190706",'count' => 8,"ajax"=>1,"token"=>$this->token,"lang"=>"zh_CN","random"=>$random_str,"f"=>"json","t"=>"ajax-response","sub"=>"update","type"=>10);
        $list=M("article")->field("id,title,seo_description,content")->order("RAND()")->limit(8)->select();
        $index=0;
 
        $fields=array("0"=>"201190690","1"=>"201190695","2"=>"201190696","3"=>"201190698","4"=>"201190701","5"=>"201190702","6"=>"201190704","7"=>"201190703");
        foreach($list as $row){
            $datas["title".$index]          =   $row['title'];
            $datas["content".$index]        =   $row["content"];        //内容
            $datas["digest".$index]         =   $row["seo_description"];            //摘要
            $datas["author".$index]         =   "爱魔方";
            $datas["fileid".$index]         =   $fields[$index];
            $datas["show_cover_pic".$index] =   0;
            $datas["sourceurl".$index]      =   "http://www.aidemofang.com/Article/".encode($row['id']);
            $index++;
        }
        $this->send_data=$datas;
        return $this->curlPost($url);
    }
     
    //群发信息
    public function sendInfo(){
        $url = 'https://mp.weixin.qq.com/cgi-bin/masssend';
        $this->referer ="https://mp.weixin.qq.com/cgi-bin/masssendpage?t=mass/send&token=".$this->token."&lang=zh_CN";
 
        $random_str="0.".mt_rand(1000000000000000,9999999999999999);
        $datas = array('type' => "10",'appmsgid' => "201190706","sex"=>0,"groupid"=>"-1","synctxweibo"=>0,"synctxnews"=>0,"country"=>"","province"=>"","city"=>"","imgcode"=>"","operation_seq"=>"201191043","token"=>$this->token,"lang"=>"zh_CN","random"=>$random_str,"f"=>"json","t"=>"ajax-response","ajax"=>"1");
        $this->send_data=$datas;
        return $this->curlPost($url);        
    }
 
    /**
     * curl模拟登录的post方法
     * @param $url request地址
     * @param $header 模拟headre头信息
     * @return json
     */
    private function curlPost($url) {
        $header = array(
            'Accept:*/*',
            'Accept-Charset:GBK,utf-8;q=0.7,*;q=0.3',
            'Accept-Encoding:gzip,deflate,sdch',
            'Accept-Language:zh-CN,zh;q=0.8',
            'Connection:keep-alive',
            'Host:'.$this->host,
            'Origin:'.$this->origin,
            'Referer:'.$this->referer,
            'X-Requested-With:XMLHttpRequest'
        );
        $curl = curl_init(); //启动一个curl会话
        curl_setopt($curl, CURLOPT_URL, $url); //要访问的地址
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header); //设置HTTP头字段的数组
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); //对认证证书来源的检查
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1); //从证书中检查SSL加密算法是否存在
        curl_setopt($curl, CURLOPT_USERAGENT, $this->useragent); //模拟用户使用的浏览器
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); //使用自动跳转
        curl_setopt($curl, CURLOPT_AUTOREFERER, 1); //自动设置Referer
        curl_setopt($curl, CURLOPT_POST, 1); //发送一个常规的Post请求
        curl_setopt($curl, CURLOPT_POSTFIELDS, $this->send_data); //Post提交的数据包
        curl_setopt($curl, CURLOPT_COOKIE, $this->cookie); //读取储存的Cookie信息
        curl_setopt($curl, CURLOPT_TIMEOUT, 30); //设置超时限制防止死循环
        curl_setopt($curl, CURLOPT_HEADER, $this->getHeader); //显示返回的Header区域内容
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); //获取的信息以文件流的形式返回
        $result = curl_exec($curl); //执行一个curl会话
        curl_close($curl); //关闭curl
        return $result;
    }
   
}
?>