Share An interface example for sending a keyword to the WeChat public platform to automatically reply to Baidu images

Source: Internet
Author: User
A message processing interface written in PHP, you can also port it to your project. Function method for retrieving Baidu Image API search results in JSON format
Do not directly copy the following code and escape it. Download the attachment.

A message processing interface written in PHP, you can also port it to your project.
Function method for retrieving Baidu Image API search results in JSON format
<? Php
$ Wx = new wxapi;
$ Wx-> callme (); // executes message processing (verify the interface first)
Class wxapi {
Public function callme (){
// Process the sent content
$ PostStr = file_get_contents ("php: // input ");
// Extract post data
If (! Empty ($ postStr )){
$ PostObj = simplexml_load_string ($ postStr, 'simplexmlelement', LIBXML_NOCDATA );
$ Fu = $ postObj-> FromUserName ."";
$ Tu = $ postObj-> ToUserName ."";
$ Type = $ postObj-> MsgType;
$ Time = $ postObj-> CreateTime;
If ($ type = 'text '){
// Text message
$ Msg = trim ($ postObj-> Content );
$ Data = $ this-> find_pic ($ msg, 4 );
If (empty ($ data )){
$ This-> tpl ($ fu, $ tu, 'no result found ', 'text ');
Exit;
}
$ This-> tpl ($ fu, $ tu, $ data );
}
}
}
// Message template
Function tpl ($ fu, $ tu, $ data, $ type = 'news', $ flg = 0 ){
If ($ type = 'news '){
$ Num = count ($ data); // count
If ($ num> 1) {// return multiple
$ Add = $ this-> news_add ($ data );
$ Tpl ="
".$fu."
".$tu."
". $ _ SERVER ['request _ time']."
news
%s
". $ Num ."

". $ Add ."

". $ Flag ."
";
Echo $ tpl;
} Else {// return a single entry
$ Tpl ="
".$fu."
".$tu."
". $ _ SERVER ['request _ time']."
news
%s
1


<! [CDATA [". $ data [0] ['frompagetitleenc']."]>
来自百度搜索
".$data[0]['objURL']."
".$data[0]['fromURL']."


". $ Flag ."
";
Echo $ tpl;
}
} Elseif ($ type = 'text '){
$ Tpl ="
".$fu."
".$tu."
". $ _ SERVER ['request _ time']."
text
".$data."
". $ Flag ."
";
Echo $ tpl;
}
}
// Append Template
Function news_add ($ data ){
$ Add = "";
Foreach ($ data as $ k ){
$ Add. ="
<! [CDATA [". $ k ['frompagetitleenc']."]>
来自百度搜索
".$k['objURL']."
".$k['fromURL']."
";
}
Return $ add;
}
// Baidu Image Search API
Function find_pic ($ keyword, $ num = 1 ){
$ Keyword = iconv ('utf-8', 'gbk', $ keyword );
$ Page = 1; // page?
$ Count = $ num; // The number of results displayed on each page
$ Url = "http://image.baidu.com/I? Tn = baiduimagejson & ct = 201326592 & cl = 2 & lm =-1 & st =-1 & fm = result & fr = & sf = 1 & fmq = 1349413075627_R & pv = & ic = 0 & nc = 1 & z = & se = 1 & showtab = 0 & fb = 0 & width = & height = & face = 0 & istype = 2 & word =". $ keyword. "& rn = ". $ count. "& pn = ". $ page;
$ Result = file_get_contents ($ url );
$ Result = iconv ('gbk', 'utf-8', $ result );
// Now have some fun with the results...
$ A = json_decode ($ result, true );
$ Data = $ a ['data'];
Unset ($ data [$ count]); // removes the last empty array.
$ C = count ($ data); // count
If ($ c >=$ num) {// The number of returned results that meet the specified value
// Shuffle ($ data );
Return $ data;
} Else {// no result or does not match the returned quantity
$ Id = array_rand ($ data );
Return $ data [$ id];
}
}
}

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.