技术文摘

每天热点新闻聚合站php采集教程

作者:雨祺   发表于:
浏览:66次    字数:4922  电脑原创
级别:站长   总稿:76 篇,  月稿:2
每天热点新闻聚合站php采集教程。怎么获得他们的接口呢?自己找到他们对应的接口就行了。小编找了几个。先看看封装的几个效果:
  1. 比如百度的:https://www.wenyunfang.com/ecmsapi/duomeiti/hot/baidu/ 
  2. 比如抖音的:https://www.wenyunfang.com/ecmsapi/duomeiti/hot/douyin/ 
  3. 比如知乎的:https://www.wenyunfang.com/ecmsapi/duomeiti/hot/zhihu/ 
细心的都看见了,就是最后的请求参数变了而已就能获取到他们对应的热点新闻了。(备注:本接口只做演示用,不不定期删除的哈)不啰嗦开始上代码。有点长直接复制过去调试就行
  1. <?php 
  2. // Curl请求函数 
  3. function Curl($url, $header = ["accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9","Accept-Encoding: gzip, deflate, br","Accept-Language: zh-CN,zh;q=0.9","Connection: keep-alive","User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_1 like Mac OS X) AppleWebKit/603.1.30 (KHTML, like Gecko) Version/10.0 Mobile/14E304 Safari/602.1"], $cookie = null, $refer = 'https://www.baidu.com'){ 
  4. $ip = rand(0, 255) . '.' . rand(0, 255) . '.' . rand(0, 255) . '.' . rand(0, 255); 
  5. $header[] = "CLIENT-IP:" . $ip; 
  6. $header[] = "X-FORWARDED-FOR:" . $ip; 
  7. $ch = curl_init(); 
  8. curl_setopt($ch, CURLOPT_URL, $url); 
  9. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);  
  10. curl_setopt($ch, CURLOPT_COOKIE, $cookie);  
  11. curl_setopt($ch, CURLOPT_REFERER,  $refer); 
  12. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
  13. curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');  
  14. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);  
  15. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);  
  16. curl_setopt($ch, CURLOPT_TIMEOUT, 5);  
  17. $output = curl_exec($ch); 
  18. curl_close($ch); 
  19. return $output; 
  20. // 少数派 热榜 
  21. function sspai(){ 
  22. $jsonRes = json_decode(Curl('https://sspai.com/api/v1/article/tag/page/get?limit=100000&tag=%E7%83%AD%E9%97%A8%E6%96%87%E7%AB%A0'nullnull"https://sspai.com"), true); 
  23. $tempArr = []; 
  24. foreach ($jsonRes['data'] as $k => $v) { 
  25. array_push($tempArr, [ 
  26. 'index' => $k + 1, 
  27. 'title' => $v['title'], 
  28. 'createdAt' => date('Y-m-d', $v['released_time']), 
  29. 'other' => $v['author']['nickname'], 
  30. 'like_count' => $v['like_count'], 
  31. 'comment_count' => $v['comment_count'], 
  32. 'url' => 'https://sspai.com/post/' . $v['id'], 
  33. 'mobilUrl' => 'https://sspai.com/post/' . $v['id'
  34. ]); 
  35. return [ 
  36. 'success' => true
  37. 'title' => '少数派'
  38. 'subtitle' => '热榜'
  39. 'update_time' => date('Y-m-d h:i:s', time()), 
  40. 'data' => $tempArr 
  41. ]; 
  42. // 抖音 热搜榜 
  43. function douyin(){ 
  44. $jsonRes = json_decode(Curl('https://www.iesdouyin.com/web/api/v2/hotsearch/billboard/word/'nullnull"https://www.douyin.com"), true); 
  45. $tempArr = []; 
  46. foreach ($jsonRes['word_list'] as $k => $v) { 
  47. array_push($tempArr, [ 
  48. 'index' => $k + 1, 
  49. 'title' => $v['word'], 
  50. 'hot' => round($v['hot_value'] / 10000, 1) . '万'
  51. 'url' => 'https://www.douyin.com/search/' . urlencode($v['word']), 
  52. 'mobilUrl' => 'https://www.douyin.com/search/' . urlencode($v['word']) 
  53. ]); 
  54. return [ 
  55. 'success' => true
  56. 'title' => '抖音'
  57. 'subtitle' => '热搜榜'
  58. 'update_time' => date('Y-m-d h:i:s', time()), 
  59. 'data' => $tempArr 
  60. ]; 
  61. // 哔哩哔哩 全站日榜 
  62. function bilibili_rankall(){ 
  63. $jsonRes = json_decode(Curl('https://api.bilibili.com/x/web-interface/ranking/v2?rid=0&type=all'nullnull"https://www.bilibili.com"), true); 
  64. $tempArr = []; 
  65. foreach ($jsonRes['data']['list'] as $k => $v) { 
  66. array_push($tempArr, [ 
  67. 'index' => $k + 1, 
  68. 'title' => $v['title'], 
  69. 'pic' => $v['pic'], 
  70. 'desc' => $v['desc'], 
  71. 'hot' => round($v['stat']['view'] / 10000, 1) . '万'
  72. 'url' => $v['short_link'], 
  73. 'mobilUrl' => $v['short_link'
  74. ]); 
  75. return [ 
  76. 'success' => true
  77. 'title' => '哔哩哔哩'
  78. 'subtitle' => '全站日榜'
  79. 'update_time' => date('Y-m-d h:i:s', time()), 
  80. 'data' => $tempArr 
  81. ]; 
  82. // 哔哩哔哩 热搜榜 
  83. function bilibili_hot(){ 
  84. $jsonRes = json_decode(Curl('https://app.bilibili.com/x/v2/search/trending/ranking'nullnull"https://www.bilibili.com"), true); 
  85. $tempArr = []; 
  86. foreach ($jsonRes['data']['list'] as $k => $v) { 
  87. array_push($tempArr, [ 
  88. 'index' => $v['position'], 
  89. 'title' => $v['keyword'], 
  90. 'url' => 'https://search.bilibili.com/all?keyword=' . $v['keyword'] . '&order=click'
  91. 'mobilUrl' => 'https://search.bilibili.com/all?keyword=' . $v['keyword'] . '&order=click' 
  92. ]); 
  93. return [ 
  94. 'success' => true
  95. 'title' => '哔哩哔哩'
  96. 'subtitle' => '热搜榜'
  97. 'update_time' => date('Y-m-d h:i:s', time()), 
  98. 'data' => $tempArr 
  99. ]; 
  100. // 知乎热榜  热度 
  101. function zhihuHot(){ 
  102. $jsonRes = json_decode(Curl('https://www.zhihu.com/api/v3/feed/topstory/hot-lists/total?limit=50&desktop=true'nullnull"https://www.zhihu.com"), true); 
  103. $tempArr = []; 
  104. foreach ($jsonRes['data'] as $k => $v) { 
  105. preg_match('/\d+/',  $v['detail_text'], $hot); 
  106. array_push($tempArr, [ 
  107. 'index' => $k + 1, 
  108. 'title' => $v['target']['title'], 
  109. 'hot' => $hot[0] . '万'
  110. 'url' => 'https://www.zhihu.com/question/' . urlencode($v['target']['id']), 
  111. 'mobilUrl' => 'https://www.zhihu.com/question/' . urlencode($v['target']['id']) 
  112. ]); 
  113. return [ 
  114. 'success' => true
  115. 'title' => '知乎热榜'
  116. 'subtitle' => '热度'
  117. 'update_time' => date('Y-m-d h:i:s', time()), 
  118. 'data' => $tempArr 
  119. ]; 
  120. // 微博 热搜榜 
  121. function wbresou(){ 
  122. $_md5 = md5(time()); 
  123. $cookie = "Cookie: {$_md5}:FG=1"
  124. $jsonRes = json_decode(Curl('https://weibo.com/ajax/side/hotSearch'null, $cookie, "https://s.weibo.com"), true); 
  125. $tempArr = []; 
  126. foreach ($jsonRes['data']['realtime'] as $k => $v) { 
  127. array_push($tempArr, [ 
  128. 'index' => $k + 1, 
  129. 'title' => $v['note'], 
  130. 'hot' => round($v['num'] / 10000, 1) . '万'
  131. 'url' => "https://s.weibo.com/weibo?q=" . $v['note'] . "&Refer=index"
  132. 'mobilUrl' => "https://s.weibo.com/weibo?q=" . $v['note'] . "&Refer=index" 
  133. ]); 
  134. return [ 
  135. 'success' => true
  136. 'title' => '微博'
  137. 'subtitle' => '热搜榜'
  138. 'update_time' => date('Y-m-d h:i:s', time()), 
  139. 'data' => $tempArr 
  140. ]; 
  141. // 百度热点 指数 
  142. function baiduredian(){ 
  143. $_resHtml = str_replace(["\n""\r"" "], '', Curl('https://top.baidu.com/board?tab=realtime'null)); 
  144. preg_match('/<!--s-data:(.*?)-->/', $_resHtml, $_resHtmlArr); 
  145. $jsonRes = json_decode($_resHtmlArr[1], true); 
  146. $tempArr = []; 
  147. foreach ($jsonRes['data']['cards'] as $v) { 
  148. foreach ($v['content'] as $k => $_v) { 
  149. array_push($tempArr, [ 
  150. 'index' => $k + 1, 
  151. 'title' => $_v['word'], 
  152. 'desc' => $_v['desc'], 
  153. 'pic' => $_v['img'], 
  154. 'url' => $_v['url'], 
  155. 'hot' => round($_v['hotScore'] / 10000, 1) . '万'
  156. 'mobilUrl' => $_v['appUrl'
  157. ]); 
  158. return [ 
  159. 'success' => true
  160. 'title' => '百度热点'
  161. 'subtitle' => '指数'
  162. 'update_time' => date('Y-m-d h:i:s', time()), 
  163. 'data' => $tempArr 
  164. ]; 
  165. $_type = $_GET['enews'] ? $_GET['enews'] : ''
  166. switch ($_type) { 
  167.     case 'baidu'
  168.         $_res = baiduredian(); 
  169.         break
  170.     case 'zhihu'
  171.         $_res = zhihuHot(); 
  172.         break
  173.     case 'weibo'
  174.         $_res = wbresou(); 
  175.         break
  176.     case 'bilihot'
  177.         $_res = bilibili_hot(); 
  178.         break
  179.     case 'biliall'
  180.         $_res = bilibili_rankall(); 
  181.         break
  182.     case 'douyin'
  183.         $_res = douyin(); 
  184.         break
  185.     case 'sspai'
  186.         $_res = sspai(); 
  187.         break
  188.     default
  189.         $_res = ['success' => false'message' => '参数不完整']; 
  190.         break
  191. echo json_encode($_res, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); 
以上代码命名为hot.php。那如请求百度的就是hot.php?enews=baidu
怎么与小编的不是一样的呢 ?因为小编的接口地址已经被伪静态了。其实get请求字段都是enews

【审核人:站长】

收藏   加好友   生成海报   分享
点赞(0)
打赏
Tags:PHP新闻教程

发布者资料

热门文章

技术文摘

查看更多技术文摘
首页
栏目
搜索
会员
投稿