According to the message data format of the sending message interface in the enterprise number developer documentation, to send messages to enterprise number members, JSON data must be sent to the specified URL containing ACCESS_TOKEN in POST mode. What I want to achieve is to query the database at intervals of time according to the query results to specific...
According to the message data format of the sending message interface in the enterprise number developer documentation, to send messages to enterprise number members, JSON data must be sent to the specified URL containing ACCESS_TOKEN in POST mode.
What I want to achieve is to send messages to specific members after querying the database at intervals of time based on the query results.
I wrotecurl
The command can successfully post json data, and I also receive information on my mobile phone, indicating that I have no problem with the Document Understanding and data format. However, in this method, the obtained ACCESS_TOKEN hard code must be included in the command line, and the TOKEN has a time limit. Therefore, it can only be used for testing and cannot be used in the production environment.
In the production environment, I want to write a PHP/JAVASCRIPT program to send messages, and then use CRON JOB on the server to regularly execute the PHP program. However, due to insufficient understanding of the POST principle, many problems are encountered in the specific implementation process.
First, if we only consider the feasibility, can we use jQuery's ajax method to implement it?
In my experiment, I first used PHP to get the correct TOKEN, constructed the URL, and then passed the URL value to the javascript variable url. in the console, I tried
$.ajax({ type: "POST", url: url, data: '{"touser":"Jacklyn","msgtype":"text","agentid":23,"text":{"content":"test message"}}', success: function(){}, dataType: "json", contentType : "application/json"});
The console returns a cross domain error. I understand that due to cross domain problems, I cannot see the returned success or failure information.
But I didn't receive any messages on my phone, so it should have failed. To check what data is sent by the above code, I use another file to receive data:
'; var_dump($_POST); echo "
"; ?>
However, in the object. responsText returned by $. ajax, you can see that the POST result is
array(0) {}
If I remove datetype and contenttype from the AJAX method, the correct data can be seen in responseText.Therefore, the first question is, if the JSON-format data sent by $. ajax cannot be received by $ _ POST, how can the server read the sent data?
In addition, I have read some documents about POST. if I do not understand the error, the information transmitted by POST is actually composed of two parts: HEADER and DATA; I also searched for some articles about how to use php post json data. I am not very familiar with reading this article. it seems that most of the articles are about to control the HEADER to a certain extent before POST JSON, does this mean javascript cannot be implemented?
Finally, I use the following functions:
function gotoUrl(path, params, method) { //Null check method = method || "post"; // Set method to post by default if not specified. // The rest of this code assumes you are not using a library. // It can be made less wordy if you use one. var form = document.createElement("form"); form.setAttribute("method", method); form.setAttribute("action", path); //Fill the hidden form if (typeof params === 'string') { var hiddenField = document.createElement("input"); hiddenField.setAttribute("type", "hidden"); hiddenField.setAttribute("name", 'data'); hiddenField.setAttribute("value", params); form.appendChild(hiddenField); } else { for (var key in params) { if (params.hasOwnProperty(key)) { var hiddenField = document.createElement("input"); hiddenField.setAttribute("type", "hidden"); hiddenField.setAttribute("name", key); if(typeof params[key] === 'object'){ hiddenField.setAttribute("value", JSON.stringify(params[key])); } else{ hiddenField.setAttribute("value", params[key]); } form.appendChild(hiddenField); } } } document.body.appendChild(form); form.submit();}
Simulate a form to submit data, and then I try
GotoUrl (url, '{"touser": "shenkwen", "msgtype": "text", "agentid": 23, "text": {"content ": "test message"} ') if a debug = 1 parameter is added to the url, postdata is displayed. the postdata of the above code is as follows:
Data = % 7B % 22 touser % 22% 3A % 22 shenkwen % 22% 2C % 22 msgtype % 22% 3A % 22 text % 22% 2C % 22 agentid % 22% 3A23% 2C % 22 text % 22% 3A % 7B % 22 content % 22% 3A % 22 test + message % 22% 7D % 7D
It looks like urlencode is the json string. does this mean that the json data required in this scenario cannot be submitted in the form mode at all?
Reply content:
According to the message data format of the sending message interface in the enterprise number developer documentation, to send messages to enterprise number members, JSON data must be sent to the specified URL containing ACCESS_TOKEN in POST mode.
What I want to achieve is to send messages to specific members after querying the database at intervals of time based on the query results.
I wrotecurl
The command can successfully post json data, and I also receive information on my mobile phone, indicating that I have no problem with the Document Understanding and data format. However, in this method, the obtained ACCESS_TOKEN hard code must be included in the command line, and the TOKEN has a time limit. Therefore, it can only be used for testing and cannot be used in the production environment.
In the production environment, I want to write a PHP/JAVASCRIPT program to send messages, and then use CRON JOB on the server to regularly execute the PHP program. However, due to insufficient understanding of the POST principle, many problems are encountered in the specific implementation process.
First, if we only consider the feasibility, can we use jQuery's ajax method to implement it?
In my experiment, I first used PHP to get the correct TOKEN, constructed the URL, and then passed the URL value to the javascript variable url. in the console, I tried
$.ajax({ type: "POST", url: url, data: '{"touser":"Jacklyn","msgtype":"text","agentid":23,"text":{"content":"test message"}}', success: function(){}, dataType: "json", contentType : "application/json"});
The console returns a cross domain error. I understand that due to cross domain problems, I cannot see the returned success or failure information.
But I didn't receive any messages on my phone, so it should have failed. To check what data is sent by the above code, I use another file to receive data:
'; var_dump($_POST); echo "
"; ?>
However, in the object. responsText returned by $. ajax, you can see that the POST result is
array(0) {}
If I remove datetype and contenttype from the AJAX method, the correct data can be seen in responseText.Therefore, the first question is, if the JSON-format data sent by $. ajax cannot be received by $ _ POST, how can the server read the sent data?
In addition, I have read some documents about POST. if I do not understand the error, the information transmitted by POST is actually composed of two parts: HEADER and DATA; I also searched for some articles about how to use php post json data. I am not very familiar with reading this article. it seems that most of the articles are about to control the HEADER to a certain extent before POST JSON, does this mean javascript cannot be implemented?
Finally, I use the following functions:
function gotoUrl(path, params, method) { //Null check method = method || "post"; // Set method to post by default if not specified. // The rest of this code assumes you are not using a library. // It can be made less wordy if you use one. var form = document.createElement("form"); form.setAttribute("method", method); form.setAttribute("action", path); //Fill the hidden form if (typeof params === 'string') { var hiddenField = document.createElement("input"); hiddenField.setAttribute("type", "hidden"); hiddenField.setAttribute("name", 'data'); hiddenField.setAttribute("value", params); form.appendChild(hiddenField); } else { for (var key in params) { if (params.hasOwnProperty(key)) { var hiddenField = document.createElement("input"); hiddenField.setAttribute("type", "hidden"); hiddenField.setAttribute("name", key); if(typeof params[key] === 'object'){ hiddenField.setAttribute("value", JSON.stringify(params[key])); } else{ hiddenField.setAttribute("value", params[key]); } form.appendChild(hiddenField); } } } document.body.appendChild(form); form.submit();}
Simulate a form to submit data, and then I try
GotoUrl (url, '{"touser": "shenkwen", "msgtype": "text", "agentid": 23, "text": {"content ": "test message"} ') if a debug = 1 parameter is added to the url, postdata is displayed. the postdata of the above code is as follows:
Data = % 7B % 22 touser % 22% 3A % 22 shenkwen % 22% 2C % 22 msgtype % 22% 3A % 22 text % 22% 2C % 22 agentid % 22% 3A23% 2C % 22 text % 22% 3A % 7B % 22 content % 22% 3A % 22 test + message % 22% 7D % 7D
It looks like urlencode is the json string. does this mean that the json data required in this scenario cannot be submitted in the form mode at all?
ContentType can also be specified as urlencode.