WeChat PHP custom Menu

Source: Internet
Author: User
Tags php write
I applied for a public platform test account, has passed the verification, after the attention with my number to the test account to send messages can get the correct response of the test account, I now want to implement the custom menu, the following replace my test account AppID and secret, I entered the test account did not see the custom menu, Where is the problem, please?

 Post ($WECHATOBJ->get_access_token (), $data), class Wechatcallbackapitest{public function Get_access_token () {$j        Son=http_request_json ($token _url);          $data =json_decode ($json, true);          if ($data [' Access_token ']) {return $data [' Access_token '];          }else{return "Get access_token error";          }} Public Function Http_request_json ($url) {$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);          $result = curl_exec ($ch);          Curl_close ($ch);        return $result; Public Function post ($access _token, $jsonData) {$ch = Curl_init ("Https://api.weixin.qq.com/cgi-bin/menu/create? Access_token= ". $access _token); curl_setopt ($ch, Curlopt_post, 1); curl_setopt ($ch, Curlopt_customrequest," POST "); curl_setopt ($ch, Curlopt_postfields, $jsonData); curl_setopt ($ch, Curlopt_returntransfer, True), curl_setopt ($ch, Curlopt_httpheader, Array (' Content-type:application/json '); $result = Curl_exec ($ch); Curl_close ($ch); return $result;}? >


Reply to discussion (solution)

There is one more question, how do I debug this php development, where I can see the log on the server (that is, echo-like value)? I'm a server built with XAMPP.

I remember the menu in the official documentation for example
As for debugging, it should be official to throw the wrong number for your judgment.

Just 2 questions have been found, one is $data = here should be a single quotation mark, the other Http_request_json to $this->http_request_json, but still no, changed the 2 questions

I remember the menu in the official documentation for example
As for debugging, it should be official to throw the wrong number for your judgment.
The official error should be on the server Ah, he does not throw directly to the interface, I just asked me where to see the server in the error


I remember the menu in the official documentation for example
As for debugging, it should be official to throw the wrong number for your judgment.
The official error should be on the server Ah, he does not throw directly to the interface, I just asked me where to see the server in the error

According to my experience, there should be a callback after the API call ... Callback

There is one more question, how do I debug this php development, where I can see the log on the server (that is, echo-like value)? I'm a server built with XAMPP.
Debugging on the developer platform has a platform to provide debugging, but I feel it is best to do the program of the wrong (exception) processing, this will be based on the debug platform information can know where their problems. I personally like PHP, this is my blog http://www.ihuxu.com, I hope to communicate more.

1. First look at this method, whether you get the token value.

$WECHATOBJ->get_access_token ()


2. When the custom menu is sent again, what is the returned data? Follow the error code to find out where it went wrong.

Obviously, I don't know how you get the token value from this method? Where did $token _url come from?
  Public Function Get_access_token () {          $json =http_request_json ($token _url);        $data =json_decode ($json, true);          if ($data [' Access_token ']) {              return $data [' Access_token '];          } else{              return "get Access_token error";          }             }  


There is one more question, how do I debug this php development, where I can see the log on the server (that is, echo-like value)? I'm a server built with XAMPP.
Debugging on the developer platform has a platform to provide debugging, but I feel it is best to do the program of the wrong (exception) processing, this will be based on the debug platform information can know where their problems. I personally like PHP, this is my blog http://www.ihuxu.com, I hope to communicate more.
Now the problem is that I debug on the official web, the hint is successful, but my client does not see

1. First look at this method, whether you get the token value.

$WECHATOBJ->get_access_token ()


2. When the custom menu is sent again, what is the returned data? Follow the error code to find out where it went wrong.

Obviously, I don't know how you get the token value from this method? Where did $token _url come from?
  Public Function Get_access_token () {          $json =http_request_json ($token _url);        $data =json_decode ($json, true);          if ($data [' Access_token ']) {              return $data [' Access_token '];          } else{              return "get Access_token error";          }             }  

Token_url on the top of my post, I have a statement.

If you perform a return that is successful, it takes 24 hours or you cancel the attention and re-focus and you can see it.

If you perform a return that is successful, it takes 24 hours or you cancel the attention and re-focus and you can see it.
I now print directly on the server, know the place of the problem, but do not know how to change

Public Function post ($token, $jsonData) {echo $token; $MENU _url = "Https://api.weixin.qq.com/cgi-bin/menu/create? Access_token= ". $token; $ch = Curl_init (); curl_setopt ($ch, Curlopt_url, $MENU _url); curl_setopt ($ch, Curlopt_ Customrequest, "POST"); curl_setopt ($ch, Curlopt_ssl_verifypeer, FALSE) curl_setopt ($ch, Curlopt_ssl_verifyhost, FALSE); curl_setopt ($ch, Curlopt_useragent, ' mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0) curl_setopt ($ch, curlopt_followlocation, 1); curl_setopt ($ch, Curlopt_autoreferer, 1); Curl_setopt ($ CH, curlopt_postfields, $jsonData); curl_setopt ($ch, Curlopt_returntransfer, true); $result = Curl_exec ($ch); if (curl_ errno ($ch)) {echo ' Error ' Curl_error ($ch);} Curl_close ($ch); echo $result;//var_dump ($result); return $result;}

This is the way I create the menu now, I print the next, token is correctly obtained, but the menu is not created successfully, the system's print is
{"Errcode":-1, "errmsg": "System Error"}
This errorcode in the official see the explanation is the system busy, but I think it should not be the problem, because now someone using Java to create is OK, I think this piece of PHP write a problem, who help to see

Create Menu function CreateMenu ($data) {$ch = Curl_init (); curl_setopt ($ch, Curlopt_url, "Https://api.weixin.qq.com/cgi-bin /menu/create?access_token= ". Access_token); curl_setopt ($ch, Curlopt_customrequest, "POST"); curl_setopt ($ch, Curlopt_ssl_verifypeer, FALSE); curl_setopt ($ch, Curlopt_ssl_verifyhost, FALSE); curl_setopt ($ch, Curlopt_useragent, ' mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0) '); curl_setopt ($ch, curlopt_followlocation, 1); curl_setopt ($ch, Curlopt_autoreferer, 1); curl_setopt ($ch, Curlopt_postfields, $data); curl_setopt ($ch, Curlopt_returntransfer, true); $tmpInfo = curl_exec ($ch); if (Curl_errno ($ch)) {  return curl_error ($ch);} curl_close ($ch); return $tmpInfo;}

Create Menu function CreateMenu ($data) {$ch = Curl_init (); curl_setopt ($ch, Curlopt_url, "Https://api.weixin.qq.com/cgi-bin /menu/create?access_token= ". Access_token); curl_setopt ($ch, Curlopt_customrequest, "POST"); curl_setopt ($ch, Curlopt_ssl_verifypeer, FALSE); curl_setopt ($ch, Curlopt_ssl_verifyhost, FALSE); curl_setopt ($ch, Curlopt_useragent, ' mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0) '); curl_setopt ($ch, curlopt_followlocation, 1); curl_setopt ($ch, Curlopt_autoreferer, 1); curl_setopt ($ch, Curlopt_postfields, $data); curl_setopt ($ch, Curlopt_returntransfer, true); $tmpInfo = curl_exec ($ch); if (Curl_errno ($ch)) {  return curl_error ($ch);} curl_close ($ch); return $tmpInfo;}

What's the difference between you and me? $token This is a variable ah, is obtained from other places, you are let me define a global variable, the token value recorded? It's no different.

There is one more question, how do I debug this php development, where I can see the log on the server (that is, echo-like value)? I am using XAMPP to build the server Error_log ("Content", 3, "Log.txt");
The first parameter is what you want to enter,
The second parameter does not change,
The third parameter is the name of the log, you can add a path such as "C:\log.txt" you can see this file on the C drive.

menu, I wrote a small tool, you can use, you can add QQ970871674

Did you echo it when you were doing it?

Did you echo it when you were doing it?
I'll re-post the full code.

 Post ($WECHATOBJ->get_access_token (), $data);//$WECHATOBJ->responsemsg (); class Wechatcallbackapitest{public        function valid () {$echoStr = $_get["Echostr"];        Valid signature, option if ($this->checksignature ()) {echo $echoStr;        Exit }}public function Get_access_token () {$json = $this->http_request_json ("https://api.weixin.qq.com/cgi-bin/to          ken?grant_type=client_credential&appid=***&secret=*** ");          $data =json_decode ($json, true);          if ($data [' Access_token ']) {return $data [' Access_token '];          }else{return "Get access_token error";          }} Public Function Http_request_json ($url) {$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); $result = curl_exec ($ch);         Curl_close ($ch);        return $result; Public Function post ($token, $jsonData) {echo $token; $MENU _url = "Https://api.weixin.qq.com/cgi-bin/menu/create? Access_token= ". $token; $ch = Curl_init (); curl_setopt ($ch, Curlopt_url, $MENU _url); curl_setopt ($ch, Curlopt_ Customrequest, "POST"); curl_setopt ($ch, Curlopt_ssl_verifypeer, False) curl_setopt ($ch, Curlopt_ssl_verifyhost, FALSE); curl_setopt ($ch, Curlopt_useragent, ' mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0) curl_setopt ($ch, curlopt_followlocation, 1); curl_setopt ($ch, Curlopt_autoreferer, 1); Curl_setopt ($ CH, curlopt_postfields, $jsonData); curl_setopt ($ch, Curlopt_returntransfer, true); $result = Curl_exec ($ch); if (curl_ errno ($ch)) {echo ' Error ' Curl_error ($ch);} Curl_close ($ch); Echo $result; return $result;}? >

This is the code now, in addition to the real AppID and secret was replaced by my * * *

Resolved, is caused by the Chinese menu, save the file as "UTF-8 no BOM encoding format" On the line, thank you

  • Related Article

    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.