【微信服务号开发】03.实战 回复知乎日报,返回8个最新知乎日报图文消息

实战 回复知乎日报,返回8个最新知乎日报图文消息

配合这两篇实战操作
【微信服务号开发】01.接入指南
https://segmentfault.com/a/11...
【微信服务号开发】02.文字消息和图文消息自动回复实现
https://segmentfault.com/a/11...

使用接口

  • 知乎日报接口

http://news-at.zhihu.com/api/...

使用php的get请求知乎日报接口curl 类实现

<?php
class Curl
{
   /**
     * cur get
     * @param  [type] $url [description]
     * @return [type]      [description]
     */
    public static function CurlGet($url){
      $data = '';
      $ch = curl_init();
      $header = "Accept-Charset: utf-8";
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
      curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
      // curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
      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, $data);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      $temp = curl_exec($ch);
      return $temp;
      }

}

知乎日报接口处理

<?php
require_once('Curl.php');

class Daily
{
  public static function GetDail()
  {

    $url = "http://news-at.zhihu.com/api/4/news/latest";
    $data = Curl::CurlGet($url);
    $data = json_decode($data);
    $data = self::ObjectArray($data);
    // print_r($data);
    return $data['stories'];
  }

  public static function ObjectArray($array)
  {
    if(is_object($array)){
      $array = (array)$array;
    }

    if(is_array($array)){
      foreach ($array as $key => $value) {
          $array[$key] = self::ObjectArray($value);
      }
    }
     return $array;
  }
}

echo '<pre>';
$data = Daily::GetDail();
print_r($data);

图文回复函数封装

function replyArticle($data){
    global $xml;
    $article = '<ArticleCount>'.count($data).'</ArticleCount>';
    $article .= '<Articles>';
    foreach ($data as $value) {
       $article .= sprintf('<item><Title><![CDATA[%s]]></Title>',$value['title']);
       $article .= sprintf('<Description><![CDATA[%s]]></Description>',$value['desc']);
       $article .= sprintf('<PicUrl><![CDATA[%s]]></PicUrl>',$value['picurl']);
       $article .= sprintf('<Url><![CDATA[%s]]></Url></item>',$value['url']);
    }
    $article .= '</Articles>';

    $str = sprintf('<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%d</CreateTime>
<MsgType><![CDATA[news]]></MsgType>%s</xml>',$xml->FromUserName,$xml->ToUserName,time(),$article);
file_put_contents('wx.log',$str."\n\n",FILE_APPEND);
echo $str;
}

判断回复内容

if ($xml->Content == '知乎日报') {
    $res = Daily::GetDail();
    $data = [];
    foreach ($res as $key => $value) {
        $data[] = [
            'title' => $value['title'],
            'url' => 'http://stark.wang',
            'picurl' => $value['images'][0],
            'desc' => $value['title']
        ];
    }
    replyArticle(array_slice($data,1,8));
}

文件结构

【微信服务号开发】03.实战 回复知乎日报,返回8个最新知乎日报图文消息

最后测试结果

【微信服务号开发】03.实战 回复知乎日报,返回8个最新知乎日报图文消息

相关推荐