首页 > PHP资讯 > 工具库 > 分享一个微信公众平台发送关键词自动回复百度图片的接口

分享一个微信公众平台发送关键词自动回复百度图片的接口

工具库

不要直接复制下面的代码,有转义。从附件下载即可。

一个用PHP写的微信消息处理接口,你也可以将它移植到自己的项目中去。
获取百度图片API搜索结果 JSON格式 的函数方法
< ?php
$wx = new wxapi;
$wx->callme();   //执行微信消息处理 (请先验证接口)
class wxapi {
        public function callme(){
                        //处理微信发送来的内容
                        $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'){
                                                //文本消息
                                                $msg = trim($postObj->Content);
                                                $data = $this->find_pic($msg,4);
                                                if(empty($data)){
                                                        $this->tpl($fu,$tu,'没有找到相关结果','text');
                                                        exit;
                                                }
                                                $this->tpl($fu,$tu,$data);
                                        }
                        }
        }
        //消息模版
        function tpl($fu,$tu,$data,$type = 'news',$flg = 0){
                if($type == 'news'){
                        $num        =        count($data);  //统计数量
                        if($num > 1){  //返回多条
                                $add = $this->news_add($data);
                                $tpl = "
          
          
           ".$_SERVER['REQUEST_TIME']."
          
          
           ".$num."
          
           ".$add."
          

           ".$flag."
          
";
        echo $tpl;
                        }else{   //返回单条
                                $tpl = "
          
          
           ".$_SERVER['REQUEST_TIME']."
          
          
           1
          
          
           <![CDATA[".$data[0]['fromPageTitleEnc']."]]>
          
           < icUrl>
          
          

          

           ".$flag."
          
";
                        echo $tpl;
                }
         }elseif($type == 'text'){
      $tpl = "
   
   
    ".$_SERVER['REQUEST_TIME']."
   
   
    ".$flag."
   
";
    echo $tpl;
    }
  }
        //追加模版
        function news_add($data){
              $add = "";
    foreach ($data as $k){
    $add .= "
      <![CDATA[".$k['fromPageTitleEnc']."]]>
     
      < icUrl>
     
     
  ";
    }
             return $add;
        }
        //百度图片搜索API
        function find_pic($keyword,$num = 1){
                $keyword        =        iconv('utf-8','gbk',$keyword);
                $page        =        1;//第几页?
                $count        =        $num; //每页显示几条结果
                $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]); //去除最后一条空数组
                $c = count($data);//统计数量
                if($c >= $num){ //符合设定的返回数量
                        //shuffle($data);
                        return $data;
                }else{ //无结果或不符合返回的数量
                        $id = array_rand($data);
                        return $data[$id];
                }
        }
}

工具库

本文由欣才IT学院整理发布,未经许可,禁止转载。
支持19不支持0