PHP is right, but only returns an array to the WeChat message.

Source: Internet
Author: User
Tags cdata
$result = mysql_query ($sql, $link); Execute Query statement

$res =array ();
while ($row = Mysql_fetch_array ($result)) {
if (condition) {
$res [] = $row ["title"]. $row ["Answer"];
}
}
return $res;
?>
After using this, PHP is right, but it becomes a separate reply to an array


Reply to discussion (solution)

Concatenate data into strings, or encode into JSON

Return Json_encode ($res);

Return Json_encode ($res);



This one also not ah, the reply is garbled

Will anyone, Master?

Before Echo Json_encode, add: Header (' content-type:application/json;charset=utf-8 ');

Before Echo Json_encode, add: Header (' content-type:application/json;charset=utf-8 ');


$sql = "SELECT * from ' record ' WHERE title like '% $keyword% '";
$result = mysql_query ($sql, $link); Execute Query statement


$res =array ();

while ($row = Mysql_fetch_array ($result))
{



$res []= $row ["title"];


}
Header (' Content-type:application/json;charset=utf-8 ');
Return Json_encode ($res);
Mysql_close ($link);
The value returned this time is []

Before Echo Json_encode, add: Header (' content-type:application/json;charset=utf-8 ');


Could it be that there was a mistake?
%s
%s
%s
%s
%s
0


Before Echo Json_encode, add: Header (' content-type:application/json;charset=utf-8 ');


$sql = "SELECT * from ' record ' WHERE title like '% $keyword% '";
$result = mysql_query ($sql, $link); Execute Query statement


$res =array ();

while ($row = Mysql_fetch_array ($result))
{



$res []= $row ["title"];


}
Header (' Content-type:application/json;charset=utf-8 ');
Return Json_encode ($res);
Mysql_close ($link);
The value returned this time is []


The return value is empty? So return directly to "Hello World"; what is the result?


Before Echo Json_encode, add: Header (' content-type:application/json;charset=utf-8 ');


Could it be that there was a mistake?
%s
%s
%s
%s
%s
0


You put 0 Remove the test.
Also: To return information, you should not use return, but Echo
    Public Function responsemsg () {//get-post data, May is due to the different environments $postStr = $GLOBALS ["Http_raw_post_data"];             Receives the outgoing XML data//extract post data if (!empty ($POSTSTR)) {//parse post to XML as an object $postobj                    $POSTOBJ = simplexml_load_string ($postStr, ' simplexmlelement ', libxml_nocdata); $fromUsername = $POSTOBJ->fromusername; User $toUsername for Request message = $POSTOBJ->tousername; The "I" public number id $keyword = Trim ($postObj->content); The message content sent by the user $time = time (); Timestamp $msgtype = ' text '; Message type: Text $TEXTTPL = "
 
   
    %s 
    %s 
 
    
    %s 
 
    
    %s 
 
    
    %s 
 
                            
                         
                         
                         
                         
                         

  
 ";                $CONTENTSTR = "Input-h view Help (= Ω =)";                $RESULTSTR = sprintf ($TEXTTPL, $fromUsername, $toUsername, $time, $msgtype, $CONTENTSTR); Echo $resultStr;



Before Echo Json_encode, add: Header (' content-type:application/json;charset=utf-8 ');


$sql = "SELECT * from ' record ' WHERE title like '% $keyword% '";
$result = mysql_query ($sql, $link); Execute Query statement


$res =array ();

while ($row = Mysql_fetch_array ($result))
{



$res []= $row ["title"];


}
Header (' Content-type:application/json;charset=utf-8 ');
Return Json_encode ($res);
Mysql_close ($link);
The value returned this time is []


The return value is empty? So return directly to "Hello World"; what is the result?


I'm sorry, but I just got the wrong comment.
The whole is like this, I don't know what's wrong.

/**
* WeChat PHP Test
*/

Define your Token
function Chaxun ($keyword) {
$dbname = ' App_yqweixiaoxi ';
$host = getenv (' http_bae_env_addr_sql_ip ');
$port = getenv (' Http_bae_env_addr_sql_port ');
$user = getenv (' Http_bae_env_ak ');
$pwd = getenv (' Http_bae_env_sk ');
/* Then call mysql_connect () to connect the server */
$link = mysql_connect (sae_mysql_host_m. ':' . Sae_mysql_port, Sae_mysql_user, Sae_mysql_pass);
if (! $link) {
Die ("Connect Server Failed:". Mysql_error ($link));
}
/* Call mysql_select_db () immediately after the connection is successful select the database you want to connect to */
if (!mysql_select_db ($dbname, $link))
{
Die ("Select Database Failed:". Mysql_error ($link));
}

mysql_query ("Set names GBK", $link);

$sql = "SELECT * from ' record ' WHERE title like '% $keyword% '";
$result = mysql_query ($sql, $link); Execute Query statement
$res =array ();
while ($row = Mysql_fetch_array ($result))
{
$res []= $row ["title"];
}
Header (' Content-type:application/json;charset=utf-8 ');
Return Json_encode ($res);
Mysql_close ($link);
}
Define ("TOKEN", "Yqweixiaoxi");
$WECHATOBJ = new Wechatcallbackapitest ();
$WECHATOBJ->responsemsg ();


Class Wechatcallbackapitest
{
Public Function Valid ()
{
$ECHOSTR = $_get["Echostr"];

Valid signature, option
if ($this->checksignature ()) {
Echo $echoStr;
Exit
}
}

Public Function responsemsg ()
{
Get post data, May is due to the different environments
$POSTSTR = $GLOBALS ["Http_raw_post_data"];

Extract Post Data
if (!empty ($POSTSTR)) {

$POSTOBJ = simplexml_load_string ($postStr, ' simplexmlelement ', libxml_nocdata);
$fromUsername = $POSTOBJ->fromusername;
$toUsername = $POSTOBJ->tousername;
$keyword = Trim ($postObj->content);
$time = time ();
$TEXTTPL = "
%s
%s
%s
%s
%s

";
if (!empty ($keyword))


{
$msgType = "text";

$CONTENTSTR = Chaxun ($keyword);

$RESULTSTR = sprintf ($TEXTTPL, $fromUsername, $toUsername, $time, $msgType, $CONTENTSTR);
Echo $resultStr;

}else{
echo "Input something ...";
}

}else {
echo "";
Exit
}
}

Private Function Checksignature ()
{
$signature = $_get["signature"];
$timestamp = $_get["timestamp"];
$nonce = $_get["nonce"];

$token = token;
$TMPARR = Array ($token, $timestamp, $nonce);
Sort ($TMPARR);
$TMPSTR = implode ($TMPARR);
$TMPSTR = SHA1 ($TMPSTR);

if ($tmpStr = = $signature) {
return true;
}else{
return false;
}
}
}

?>



Before Echo Json_encode, add: Header (' content-type:application/json;charset=utf-8 ');


$sql = "SELECT * from ' record ' WHERE title like '% $keyword% '";
$result = mysql_query ($sql, $link); Execute Query statement


$res =array ();

while ($row = Mysql_fetch_array ($result))
{



$res []= $row ["title"];


}
Header (' Content-type:application/json;charset=utf-8 ');
Return Json_encode ($res);
Mysql_close ($link);
The value returned this time is []


The return value is empty? So return directly to "Hello World"; what is the result?





Before Echo Json_encode, add: Header (' content-type:application/json;charset=utf-8 ');


$sql = "SELECT * from ' record ' WHERE title like '% $keyword% '";
$result = mysql_query ($sql, $link); Execute Query statement


$res =array ();

while ($row = Mysql_fetch_array ($result))
{



$res []= $row ["title"];


}
Header (' Content-type:application/json;charset=utf-8 ');
Return Json_encode ($res);
Mysql_close ($link);
The value returned this time is []


The return value is empty? So return directly to "Hello World"; what is the result?


This is the database.

Your table field is UTF-8, so you should mysql_query ("Set names UTF8", $link);

In addition, there is a place to change a little (change the line, but Echo's data should be in XML format):

if (!empty ($keyword)) {$msgType = "text";        $CONTENTSTR = Chaxun ($keyword); $resultStr = sprintf ($TEXTTPL, $fromUsername, $toUsername, $time, $msgType, $CONTENTSTR) ;    echo $resultStr;} else{$msgType = "text";  $CONTENTSTR = "input something ...";    $RESULTSTR = sprintf ($TEXTTPL, $fromUsername, $toUsername, $time, $msgType, $contentStr);    echo $resultStr;}

Your table field is UTF-8, so you should mysql_query ("Set names UTF8", $link);

In addition, there is a place to change a little (change the line, but Echo's data should be in XML format):

if (!empty ($keyword)) {$msgType = "text";        $CONTENTSTR = Chaxun ($keyword); $resultStr = sprintf ($TEXTTPL, $fromUsername, $toUsername, $time, $msgType, $CONTENTSTR) ;    echo $resultStr;} else{$msgType = "text";  $CONTENTSTR = "input something ...";    $RESULTSTR = sprintf ($TEXTTPL, $fromUsername, $toUsername, $time, $msgType, $contentStr);    echo $resultStr;}


All changed, or not, bored to death, return to array with return, with Echo reply to the public number suspended service

1, you are doing the application, and are all UTF-8 encoded
So you mysql_query ("Set names GBK", $link); It's not right.
To mysql_query ("Set names UTF8", $link);
2, you put the incoming Utf-8 data $keyword as GBK explanation
This causes the Chaxun function to return an empty array
Of course, when it does not exist, it also returns an empty array.
3, by
$CONTENTSTR = Chaxun ($keyword);
$RESULTSTR = sprintf ($TEXTTPL, $fromUsername, $toUsername, $time, $msgType, $CONTENTSTR);
It is known that the $contentStr should be a string, and Chaxun returns an array, so you may need to
$CONTENTSTR = Json_encode (Chaxun ($keyword));
Or
$CONTENTSTR = Join (', ', Chaxun ($keyword));

1. Use echo to reply to the public service = = "may be the code has a syntax error or error, run this script in your domain name to see if there is an error."
2, try, do not search the database, directly let $contentstr = "string"; then echo $resultStr; See if it's data extraction.

1. Use echo to reply to the public service = = "may be the code has a syntax error or error, run this script in your domain name to see if there is an error."
2, try, do not search the database, directly let $contentstr = "string"; then echo $resultStr; See if it's data extraction.


Problem solved, thank you, chef.

1, you are doing the application, and are all UTF-8 encoded
So you mysql_query ("Set names GBK", $link); It's not right.
To mysql_query ("Set names UTF8", $link);
2, you put the incoming Utf-8 data $keyword as GBK explanation
This causes the Chaxun function to return an empty array
Of course, when it does not exist, it also returns an empty array.
3, by
$CONTENTSTR = Chaxun ($keyword);
$RESULTSTR = sprintf ($TEXTTPL, $fromUsername, $toUsername, $time, $msgType, $CONTENTSTR);
It is known that the $contentStr should be a string, and Chaxun returns an array, so you may need to
$CONTENTSTR = Json_encode (Chaxun ($keyword));
Or
$CONTENTSTR = Join (', ', Chaxun ($keyword));


Master, master, solve, admire.
Thank you so much.
  • 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.