In the php user-defined menu, I applied for a public platform test account, which has passed verification before. after paying attention to it, I sent a message to the test account to get the correct response from the test account, the custom menu I want to implement now uses *** instead of the appid and secret of my test account. I did not see the custom menu in the test account. what is the problem?
Post ($ wechatObj-> get_access_token (), $ data); class wechatCallbackapiTest {public function get_access_token () {$ json = http_request_json ($ token_url); $ data = Maid ($ json, true); if ($ data ['Access _ token']) {return $ data ['Access _ token'];} else {return "access 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)
Another question is, how can I debug such php Development? where can I see the log on the server (that is, the echo value )? I am a server built with xampp
I remember the menu has an example in the official document.
As for debugging, the official website will throw an error number to facilitate your judgment.
Two problems have been found just now. one is $ data = which should be put in single quotes, and the other is changed to $ this-> http_request_json, but there is still no such problem.
I remember the menu has an example in the official document.
As for debugging, the official website will throw an error number to facilitate your judgment.
The official error should be thrown on the server, but it will not be directly thrown on the interface. I just asked where I went to see the error on the server.
I remember the menu has an example in the official document.
As for debugging, the official website will throw an error number to facilitate your judgment.
The official error should be thrown on the server, but it will not be directly thrown on the interface. I just asked where I went to see the error on the server.
According to my experience, after an API is called, there should be a callback... callback.
Another question is, how can I debug such php Development? where can I see the log on the server (that is, the echo value )? I am a server built with xampp
Debugging has a platform for debugging on the developer platform. However, I feel that it is best to handle program errors by myself, in this way, you can know where your problem is based on the information on the debugging platform. I personally like PHP, this is my blog http://www.ihuxu.com, hope to communicate more.
1. check whether the token value is obtained by using this method.
$wechatObj->get_access_token()
2. when sending a custom menu, what is the returned data? Locate the error based on the error code.
Obviously, I don't know how your method gets the token value? $ Where does 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 "an error occurred while obtaining access_token ";}}
Another question is, how can I debug such php Development? where can I see the log on the server (that is, the echo value )? I am a server built with xampp
Debugging has a platform for debugging on the developer platform. However, I feel that it is best to handle program errors by myself, in this way, you can know where your problem is based on the information on the debugging platform. I personally like PHP, this is my blog http://www.ihuxu.com, hope to communicate more.
Now the problem is that I am debugging on the official website and the prompt is successful, but I cannot see the client
1. check whether the token value is obtained by using this method.
$wechatObj->get_access_token()
2. when sending a custom menu, what is the returned data? Locate the error based on the error code.
Obviously, I don't know how your method gets the token value? $ Where does 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 "an error occurred while obtaining access_token ";}}
Token_url is at the top of my post. it is declared
If the returned result is successful, it will take 24 hours. or you can ignore it and pay attention to it again.
If the returned result is successful, it will take 24 hours. or you can ignore it and pay attention to it again.
I now print it directly on the server. I know the problem, but I don't know how to change it.
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 how I create a menu now. After I print it, the token is obtained correctly, but the menu is not created successfully.
{"Errcode":-1, "errmsg": "system error "}
The official explanation of this errorcode is that the system is busy, but I think it should not be because it is OK to create a code using java. I think there is still a problem with this PHP code, who can help?
// 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, expires, FALSE); curl_setopt, 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, expires, FALSE); curl_setopt, 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. it is obtained from other places. you asked me to define a global variable and record the token value? That's no difference.
Another question is, how can I debug such php Development? where can I see the log on the server (that is, the echo value )? I set up the server error_log ("content", 3, "log.txt") using xampp ");
The first parameter is the content you want to enter,
Do not change the second parameter,
The third parameter is the log name. you can add a path such as "c: \ log.txt" to view the file on drive c.
Menu. I wrote a small tool that can be used. if you want to use it, add QQ970871674.
Did you also echo the execution?
Did you also echo the execution?
I will re-post the complete 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/token?grant_type=client_credential&appid= * ** & Secret = *** "); $ data = json_decode ($ json, true); if ($ data ['Access _ token']) {return $ data ['Access _ token'];} else {return "an error occurred while obtaining access_token";} public function http_request_json ($ url) {$ ch = curl_init (); curl_setopt ($ ch, CURLOPT_URL, $ url); curl_setopt ($ ch, hour, false); curl_setopt ($ ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt ($ ch, hour, 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, expires, 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 current code. besides the actual appid and secret, I replaced it ***.
Solved, is caused by the Chinese in the menu, save the file as "UTF-8 without BOM encoding format" on the line, thank you