人人商城

baike.php 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php
  2. $message = $this->message;
  3. $ret = preg_match('/(?:百科|定义)(.*)/i', $this->message['content'], $matchs);
  4. if(!$ret) {
  5. return $this->respText('请输入合适的格式, "百科+查询内容" 或 "定义+查询内容", 如: "百科姚明", "定义自行车"');
  6. }
  7. $word = $matchs[1];
  8. $url = 'http://wapbaike.baidu.com/searchresult/?word=%s';
  9. $url = sprintf($url, $word);
  10. $resp = ihttp_get($url);
  11. if ($resp['code'] == 200 && $resp['content']) {
  12. if(preg_match_all('/<div class="item"><h3><a href="(?P<link>.+?)">(?P<title>.+?)<\/a><\/h3><p>(?P<description>.+?)...<\/p><\/div>/', $resp['content'], $matchs)) {
  13. $ds = array();
  14. foreach($matchs['title'] as $key => $v) {
  15. $ds[] = array(
  16. 'title' => str_replace('_百度百科', '', strip_tags($v)),
  17. 'link' => 'http://wapbaike.baidu.com' . $matchs['link'][$key],
  18. 'description' => strip_tags($matchs['description'][$key])
  19. );
  20. }
  21. $news = array();
  22. $news[] = array('title' => "{$word} 的百科解释如下", 'description' => $ds[0]['description'], 'picurl' => 'http://g.hiphotos.baidu.com/baike/c0%3Dbaike180%2C5%2C5%2C180%2C60/sign=f38225303901213fdb3e468e358e5db4/9358d109b3de9c82afcae8666c81800a18d8bc3eb0356b97.jpg', 'url' => $ds[0]['link']);
  23. $cnt = min(count($ds), 8);
  24. for($i = 0; $i < $cnt; $i++) {
  25. $news[] = array(
  26. 'title' => $ds[$i]['title'],
  27. 'description' => $ds[$i]['description'],
  28. 'picurl' => '',
  29. 'url' => $ds[$i]['link']
  30. );
  31. }
  32. return $this->respNews($news);
  33. }
  34. }
  35. return $this->respText('没有找到结果, 要不换个词试试?');