最新消息: 新版网站上线了!!!

php用微信给指定用户发送消息实例

        function http_post($url, $param) {
            $data = "";
            if (is_array($param)) {
                foreach ($param as $k=>$v) {
                    $data .= '&'.$k.'='.$v;
                }
                if (strlen($data) > 0) {
                    $data = substr($data, 1, strlen($data) - 1);
                }
            } else
                $data = $param;
            $ch = curl_init();
            curl_setopt( $ch, CURLOPT_URL, $url);
            curl_setopt( $ch, CURLOPT_POST, 1 );
            curl_setopt( $ch, CURLOPT_HEADER, 0 );
            curl_setopt($ch, CURLOPT_TIMEOUT,300);   //只需要设置一个秒的数量就可以
            curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
            curl_setopt( $ch, CURLOPT_POSTFIELDS, $data);
            $return = curl_exec($ch);
            curl_close($ch);
            return $return;
        }
        $url = "https://api.weixin.qq.com/cgi-bin/token?";
        $data = array(
            'grant_type'=>'client_credential',
            'appid'=>'微信appid',
            'secret'=>'微信secret'
        );
        $file = http_post($url,$data);
        $josn = json_decode($file,true);
        $token = $josn['access_token'];
        $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$token;
        $arr = array(
            'touser'=>'o5Y-RwZtS6L7y1R1MZEtszIVp4Zc',////用户的openid
            'msgtype'=>'news',
            "news"=>array(
                'articles'=>array(
                    array(
                      'title'=>'',
                      'description'=> 'Is Really A Happy Day',
                      'url'=>'http://www.kuitao8.com/',
                      'picurl'=>'http://static.s1.thumb.lanrenchengxu.com/1/1b9f3756bb2b82d71fa69446659062a3.jpg',
                    ),
                ),
            ),


        );
        $res = http_post($url,json_encode($arr));
        print_r(json_decode($res,true));

转载请注明:谷谷点程序 » php用微信给指定用户发送消息实例