php网站能否跟微信公众号对接
需求是这样的: 普通的php网站,现在需要跟微信公众号对接,用户通过关注公众号以后,会给出一个活动链接页面,这个页面就是php网站上的页面,用户点击进入页面后,页面会获取到这个微信用户的微信号,以进行活动后续的营销动作。
根据这个需求,我的思路是,
1.建立微信连接
2.创建自定义菜单(菜单中要有跳转页面的url)
3.跳转url里要把用户的微信号作为参数带上
现在遇到的问题是:已经能够 获取access token 了,但是自定义菜单始终没有作用,在接口测试这里,一直报40001的错误,但是检测appid和appsecret都是对的,到这里就卡住了。求高人指点!
代码如下:
require_once "wx.php"; //这个页面是微信API里的示例代码define(AppId, "wxbxxxxxxx");//定义AppId define(AppSecret, "86bfxxxxxxxxxxxxx");//定义AppSecret $wechatObj = new Wechat();//实例化微信类 $creatMenu = $wechatObj->creatMenu();//创建菜单 class Wechat { private function getAccessToken() //获取access_token { $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".AppId."&secret=".AppSecret; $data = getCurl($url);//通过自定义函数getCurl得到https的内容 $resultArr = json_decode($data, true);//转为数组 echo $resultArr["access_token"]; echo '<br>'; echo $resultArr["expires_in"]; return $resultArr["access_token"];//获取access_token } public function creatMenu()//创建菜单 { $accessToken = $this->getAccessToken();//获取access_token $menuPostString = '{ "button":[ { "name":"产品介绍", "sub_button":[ { "type":"view", "name":"A", "url":"http://www.XXXX.com/fenxiaob/jianjie/soft.html" } ] }, { "name":"申请试用", "sub_button":[ { "type":"click", "name":"地接批发", "key":"dj" } ] }, { "name":"在线", "sub_button":[ { "type":"view", "name":"公司新闻", "url":"http://www.xxxxx.com/news/company/" }, ] } ] }'; $menuPostUrl = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$accessToken;//POST的url $menu = dataPost($menuPostString, $menuPostUrl);//将菜单结构体POST给微信服务器 } } function getCurl($url){//get https的内容 $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$url); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);//不输出内容 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); $result = curl_exec($ch); curl_close ($ch); return $result; } function dataPost($post_string, $url) {//POST方式提交数据 $context = array ('http' => array ('method' => "POST", 'header' => "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US) \r\n Accept: */*", 'content' => $post_string ) ); $stream_context = stream_context_create ( $context ); $data = file_get_contents ( $url, FALSE, $stream_context ); return $data; }
回复讨论(解决方案)
我记得4001只是一个错误类,还要看具体的错误信息。把详细的错误信息贴出来看看。
不出意外的话,我猜测是你没有菜单的接口权限,具体可以到公众号后台的开发者处接口权限列表里查看
参考: http://doc.okbase.net/Demon_311/archive/55555.html
token和access_token原来是不同的概念,token是在微信后台选择开发模式的时候填写的,而access_token则是通过接口调用生成的。调用的语句是:
https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
token和access_token原来是不同的概念,token是在微信后台选择开发模式的时候填写的,而access_token则是通过接口调用生成的。调用的语句是:
https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
token和access_token不同,这个我知道的,而且现在代码里也是有单独获取access_token的函数了
private function getAccessToken() //获取access_token { $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".AppId."&secret=".AppSecret; $data = getCurl($url);//通过自定义函数getCurl得到https的内容 $resultArr = json_decode($data, true);//转为数组 echo $resultArr["access_token"]; echo '<br>'; echo $resultArr["expires_in"]; return $resultArr["access_token"];//获取access_token }
晕,原来自定义菜单的代码是正确的,只不过生效要过一天以后才能看到,我还一直以为是自己哪里没做对
不需要一天的时间的,一个小时就可以了,你关闭公众号,关闭微信或退出微信,一个小时内查看,菜单就好了。
现在是另外的问题了:
通过自定义菜单,用户跳转到网站的活动页面参与抽奖,如果获取用户的微信号?
我现在实现的方式是这样:
在活动页面的js代码里,增加窗体加载时间,用ajax调用一个页面,页面的作用是调用微信获取code接口,并绑定回调的页面路径
代码如下:
<?php //require_once "../qp/conn.php";if(isset($_SESSION['user'])){ print_r($_SESSION['user']); exit;}//mysql_query("insert into test(str)values('进入getcodeurl')",$conn); //这里是做一个数据添加的测试,表示ajax调用代码正确$APPID='xxxxxxx';$REDIRECT_URI='http://www.xxxxxx.cn/callback.php';$scope='snsapi_base';$state = '123';$url='https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$APPID.'&redirect_uri='.urlencode($REDIRECT_URI).'&response_type=code&scope='.$scope.'&state='.$state.'#wechat_redirect';header("Location:".$url);?>
但是在callback.php中,并没有执行测试的数据添加,那就意味着回调绑定不成功,不知道是哪里出的问题???
<?phprequire_once "qp/conn.php";$appid = "xxxxxxxxx"; $secret = "xxxxxxxxxxxxxxxxxxxxxx"; $code = $_GET["code"]; $get_token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$secret.'&code='.$code.'&grant_type=authorization_code';$ch = curl_init();curl_setopt($ch,CURLOPT_URL,$get_token_url); curl_setopt($ch,CURLOPT_HEADER,0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 ); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); $res = curl_exec($ch); curl_close($ch); $json_obj = json_decode($res,true); //根据openid和access_token查询用户信息 $access_token = $json_obj['access_token']; $openid = $json_obj['openid']; $get_user_info_url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$openid.'&lang=zh_CN'; $ch = curl_init(); curl_setopt($ch,CURLOPT_URL,$get_user_info_url); curl_setopt($ch,CURLOPT_HEADER,0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 ); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); $res = curl_exec($ch); curl_close($ch); //解析json $user_obj = json_decode($res,true); $_SESSION['user'] = $user_obj; mysql_query("insert into test(str)values('callback:nickname=".$user_obj['nickname']."')",$conn);//这里这个sql操作是没有执行的print_r($user_obj); ?>
这样的方式可行么?或者有没有更好的方法来实现这个目的?
解决了,用http://xiaohuang.cc/post/437.html这个帖子上的方法就行了

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio

In this article, we're going to explore the notification system in the Laravel web framework. The notification system in Laravel allows you to send notifications to users over different channels. Today, we'll discuss how you can send notifications ov
