# 前言
前几天接了一个别人的作业单,用 uniapp+uview 做新闻项目,然后找了一天都没有好的接口(免费的限制一天调用 50 次,我 TM 调试都不够,还 TM 要实名你敢信?)所以干脆自己写接口,做完了就分享出来吧(仅供学习,侵权联系我,马上删)
# 获取新闻列表
这个我抓到了现成的接口,所以直接用就完了
| <?php | |
| header('Content-Type: text/html;charset=utf-8'); | |
| header('Access-Control-Allow-Origin:*'); // * 代表允许任何网址请求 | |
| header('Access-Control-Allow-Methods:POST,GET,OPTIONS,DELETE'); // 允许请求的类型 | |
| header('Access-Control-Allow-Credentials: true'); // 设置是否允许发送 cookies | |
| header('Access-Control-Allow-Headers: Content-Type,Content-Length,Accept-Encoding,X-Requested-with, Origin'); // 设置允许自定义请求头的字段 | |
| // 定义存放数据的数组 | |
| $dataArray = array(); | |
| // 访问每个接口,并将返回的 JSON 数据合并到数组中 | |
| for ($i=1;$i<=10;$i++) { | |
| $urls = 'https://mini.eastday.com/ns/api/index/typenews/0010-'.$i.'.json?callback=typenews6'; | |
| $response = file_get_contents($urls); | |
| $response = substr($response, 0, -1); | |
| $pip = 'typenews'.($i).'('; | |
| $response = str_replace($pip, '', $response); | |
| $jsonData = json_decode($response, true); | |
| for ($t=0;$t<count($jsonData);$t++){ | |
| if(!$jsonData[$t]['img169List'][0]){ | |
| }else { | |
|            // code... | |
| $arr = array( | |
| 'uk'=> $jsonData[$t]['uk'], | |
| "title"=> $jsonData[$t]["topic"], | |
| "type_zh"=> $jsonData[$t]['type_zh'], | |
| "source"=> $jsonData[$t]['source'], | |
| "show_date"=> $jsonData[$t]['show_date'], | |
| "img169List"=> $jsonData[$t]['img169List'][0], | |
| "desc"=> $jsonData[$t]['desc'], | |
| "page_count"=> $jsonData[$t]['page_count'], | |
| "leader_news"=> $jsonData[$t]['leader_news'], | |
| "original_source"=> $jsonData[$t]['original_source'], | |
| "isHot"=> $jsonData[$t]['isHot'], | |
| "is_hot"=> $jsonData[$t]['is_hot'] | |
| ); | |
| $dataArray[] = $arr; | |
|    } | |
|    } | |
|     // 将每条数据添加到数组中 | |
| } | |
| // 将数组转换为 JSON 格式 | |
| $jsonString = json_encode($dataArray, JSON_UNESCAPED_UNICODE); | |
| // 输出 JSON 数据 | |
| header('Content-Type: application/json'); | |
| echo $jsonString; | |
| ?> | 
# 获取文章内容
这个是爬取 html 格式的内容页
| <?php | |
| // 设置响应头,允许跨域请求 | |
| header('Content-Type: text/html;charset=utf-8'); | |
| header('Access-Control-Allow-Origin: *'); // * 代表允许任何网址请求 | |
| header('Access-Control-Allow-Methods: POST,GET,OPTIONS,DELETE'); // 允许请求的类型 | |
| header('Access-Control-Allow-Credentials: true'); // 设置是否允许发送 cookies | |
| header('Access-Control-Allow-Headers: Content-Type, Content-Length, Accept-Encoding, X-Requested-with, Origin'); // 设置允许自定义请求头的字段 | |
| // 获取 cid 参数 | |
| $cid = $_GET['cid']; | |
| // 初始化 cURL | |
| $ch = curl_init(); | |
| // 设置 cURL 选项 | |
| $url = 'https://mini.eastday.com/mobile/' . $cid . '.html'; | |
| curl_setopt($ch, CURLOPT_URL, $url); | |
| curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
| $response = curl_exec($ch); | |
| // 检查请求是否成功 | |
| if ($response === false) { | |
| echo 'Failed to fetch the page.'; | |
| } else { | |
|     // 使用 DOMDocument 解析网页内容 | |
| $dom = new DOMDocument(); | |
| libxml_use_internal_errors(true); // 忽略 HTML 中的错误 | |
| $dom->loadHTML($response); | |
| libxml_use_internal_errors(false); // 恢复错误处理 | |
|     // 使用 getElementById 方法找到 ID 为 "content" 的元素 | |
| $contentElement = $dom->getElementById('content'); | |
| $titleElement = $dom->getElementById('title'); | |
|     // 将 DOM 元素转换为 HTML 字符串 | |
| $html = $dom->saveHTML($contentElement); | |
|     // 使用 XPath 查询获取标题和发布时间 | |
| $xpath = new DOMXPath($dom); | |
| $title = $xpath->query('//div[@id="title"]//h1[@class="title"]')->item(0)->textContent; | |
| $publishTime = $xpath->query('//div[@id="title"]//span[@class="src"]')->item(0)->textContent; | |
|     // 构建要输出的 JSON 数据 | |
| $jsonArray = array( | |
| 'content' => $html, | |
| 'title' => $title, | |
| 'publishTime' => $publishTime | |
| ); | |
|     // 将数组转换为 JSON 格式 | |
| $jsonString = json_encode($jsonArray, JSON_UNESCAPED_UNICODE); | |
|     // 输出 JSON 数据 | |
| header('Content-Type: application/json'); | |
| echo $jsonString; | |
| } | |
| // 关闭 cURL 资源 | |
| curl_close($ch); | |
| ?> | 
最后说一句小白练手作品,大佬勿喷
还有就是侵权请联系我,马上删