微信开发入门demo

PHP 徐, 伟 103℃ 0评论

接入指南配置token的时候 echo一下 $_GET[‘echostr’];可配置成功,详情可见微信平台开发文档

1.微信demo 实例

微信开发入门demo

index.php

2.微信授权代码(可以和第一部分所联系起来)

<?php
header("Content-Type: text/html;charset=utf-8");
$code = $_GET['code'];
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx9c1456dbb22986fb&secret=cbda5bb6a92b0464dae29c1adbb22e73&code={$code}&grant_type=authorization_code ";
$result = https_request($url);
$result = json_decode($result);
//var_dump($result->access_token);
$token = $result->access_token;
$openid = $result->openid;
$end_url = "https://api.weixin.qq.com/sns/userinfo?access_token={$token}&openid={$openid}&lang=zh_CN";
$end = https_request($end_url);
var_dump($end);
$end = json_decode($end);
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);
    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;
}

3.通过mead_id 可自定义回复图片、图文消息、语音

<?php
function uploadImg($imgUrl)
{
    $appid = "wx9c1456dbb22986fb";
    $appsecret = "cbda5bb6a92b0464dae29c1adbb22e73";
    $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$appsecret}";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($ch);
    curl_close($ch);
    $jsoninfo = json_decode($output, true);
    $access_token = $jsoninfo["access_token"];
    $URL = 'http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=' . $access_token . '&type=image';
    $data = array('media' => '@' . $imgUrl);
    $result = curl_post($URL, $data);
    $data = @json_decode($result, true);
    //var_dump($data);exit;
    return $data['media_id'];
}
function curl_post($url, $data = null)
{
    //创建一个新cURL资源
    $curl = curl_init();
    //设置URL和相应的选项
    curl_setopt($curl, CURLOPT_URL, $url);
    if (!empty($data)) {
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    }
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    //执行curl,抓取URL并把它传递给浏览器
    $output = curl_exec($curl);
    //关闭cURL资源,并且释放系统资源
    curl_close($curl);
    return $output;
}
$path = dirname(__FILE__) . "/4.jpg";
$a = uploadImg($path);
return $a;

4、自定义菜单代码

<?php
$appid = "wx9c1456dbb22986fb";
$appsecret = "cbda5bb6a92b0464dae29c1adbb22e73";
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$appsecret}";
$output = https_request($url);
$jsoninfo = json_decode($output, true);
$access_token = $jsoninfo["access_token"];
$jsonmenu = '{
    "button": [
        {
            "name": "天气预报",
            "sub_button": [
                {
                    "type": "view",
                    "name": "曾云鑫请点这!!",
                    "url": "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx9c1456dbb22986fb&redirect_uri=http://xw.cooljun.cn/oauth.php&response_type=
code&scope=snsapi_userinfo&state=1#wechat_redirect"
                },
                {
                    "type": "click",
                    "name": "上海天气",
                    "key": "天气上海"
                },
                {
                    "type": "click",
                    "name": "广州天气",
                    "key": "天气广州"
                },
                {
                    "type": "click",
                    "name": "深圳天气",
                    "key": "天气深圳"
                },
                {
                    "type": "view",
                    "name": "本地天气",
                    "url": "http://m.hao123.com/a/tianqi"
                }
            ]
        },
        {
            "name": "铁头娃",
            "sub_button": [
                {
                    "type": "click",
                    "name": "公司简介",
                    "key": "company"
                },
                {
                    "type": "click",
                    "name": "趣味游戏",
                    "key": "游戏"
                },
                {
                    "type": "click",
                    "name": "讲个笑话",
                    "key": "笑话"
                }
            ]
        }
    ]
}';
$url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" . $access_token;
$result = https_request($url, $jsonmenu);
var_dump($result);
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);
    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;
}

转载请注明:cooljun小站 » 微信开发入门demo

如果你觉得这篇文章对你有帮助,请支持我继续更新网站 !捐赠本站
喜欢 (1)or分享 (0)
发表我的评论
取消评论
表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
(1)个小伙伴在吐槽
  1. cooljun
    :smile: :smile: 加油
    cooljun2017-09-21 15:49 回复