Can php websites be connected to WeChat official accounts?

Source: Internet
Author: User
Tags openid
The requirement for connecting a php website to a public account is as follows: a common php website needs to be connected to a public account. after paying attention to the public account, the user will provide an activity link page, this page is the page on the php website. after you click to enter the page, the page will get the user's number for subsequent marketing activities.

According to this requirement, my idea is,
1. establish a connection
2. create a custom menu (the url of the jump page must be included in the menu)
3. the user ID must be included as a parameter in the jump url.


The problem is that the access token can be obtained, but the custom menu does not work. in the interface test, error 40001 is always reported, however, the detection of appid and appsecret is correct, and it gets stuck here. Please kindly advise!
The code is as follows:
Require_once "wx. php "; // This page is the demo code define (AppId," wxbxxxxxxx ") in the API; // defines AppId define (AppSecret," 86 bfxxxxxxxxxxxxx "); // define AppSecret $ wechatObj = new Wechat (); // instantiate class $ creatMenu = $ wechatObj-> creatMenu (); // Create menu class Wechat {private function getAccessToken () // get access_token {$ url = "https://api.weixin.qq.com/cgi-bin/token? Grant_type = client_credential & appid = ". appId. "& secret = ". appSecret; $ data = getCurl ($ url); // Get the https content through the custom function getCurl $ resultArr = json_decode ($ data, true ); // convert to the array echo $ resultArr ["access_token"]; echo'
'; Echo $ resultArr ["expires_in"]; return $ resultArr ["access_token"]; // get access_token} public function creatMenu () // Create menu {$ accessToken = $ this-> getAccessToken (); // Obtain access_token $ menuPostString = '{"button": [{"name": "Product Introduction ", "sub_button": [{"type": "view", "name": "A", "url ":" http://www.XXXX.com/fenxiaob/jianjie/soft.html "}] },{" Name ":" Apply for trial "," sub_button ": [{" type ":" click "," name ":" wholesale ", "key": "dj"}] },{ "name": "Online", "sub_button": [{"type": "view", "name ": "Company News", "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 the menu structure to the server} function getCurl ($ url) {// get https content $ 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) {// submit data in POST mode $ 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 ;}


Reply to discussion (solution)

I remember that 4001 is only an error class, and you need to check the specific error information. Paste the detailed error information.
Unexpectedly, I guess you do not have the menu interface permission. you can view it in the interface permission list of the developer at the public account background.

Reference: http://doc.okbase.net/Demon_311/archive/55555.html

Token and access_token are different concepts. token is entered when the development mode is selected in the background, and access_token is generated by calling the interface. The called statement is:

Https://api.weixin.qq.com/cgi-bin/token? Grant_type = client_credential & appid = APPID & secret = APPSECRET

Token and access_token are different concepts. token is entered when the development mode is selected in the background, and access_token is generated by calling the interface. The called statement is:

Https://api.weixin.qq.com/cgi-bin/token? Grant_type = client_credential & appid = APPID & secret = APPSECRET



The token and access_token are different. I know this, and now the code also has a function to obtain the access_token separately.
Private function getAccessToken () // get access_token {$ url = "https://api.weixin.qq.com/cgi-bin/token? Grant_type = client_credential & appid = ". appId. "& secret = ". appSecret; $ data = getCurl ($ url); // Get the https content through the custom function getCurl $ resultArr = json_decode ($ data, true ); // convert to the array echo $ resultArr ["access_token"]; echo'
'; Echo $ resultArr ["expires_in"]; return $ resultArr ["access_token"]; // get access_token}

Dizzy, the code from the original menu definition is correct, but it will take a day to see it, and I still thought I was not doing anything right

You don't need a day. you can do it in an hour. you can close the public account, close the account, or exit. you can view the menu within one hour.

Now there is another problem:
Through the custom menu, the user jumps to the activity page of the website to participate in the lucky draw. if you get the user's number?
The current implementation method is as follows:
In the js code of the activity page, increase the loading time of the form and call a page using ajax. the function of the page is to call the get code interface and bind the page path of the callback.
The code is as follows:

 

But in callback. php, no data is added for the test, which means that the callback binding fails. I don't know what the problem is ???
 


Is this method feasible? Or is there a better way to achieve this?

Solve the problem. use http://xiaohuang.cc/post/437.htmlto access the website.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.