本教程操作环境:windows7系统、PHP8.1版、Dell G3电脑。
php怎么查询新闻头条?
基于PHP的免费新闻头条接口查询
1、开通接口
新闻头条接口服务使用的聚合数据提供的免费接口,每天可以100次免费调用。
可以通过https://www.juhe.cn/docs/api/id/235?s=cpphpcn
注册及开通。
2、新闻头条列表查询
复制代码1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?php
/**
* 聚合新闻头条列表发起请求-PHP代码
* 功能:最新新闻头条,各类社会、国内、国际、体育、娱乐、科技等资讯。
*/
// 请求的接口URL
$apiUrl = 'http://v.juhe.cn/toutiao/index';
// 请求参数
$params = [
'type' => 'top', // 新闻类型
'key' => 'xxxxxx', // 接口调用key,通过聚合平台申请开通
];
$paramsString = http_build_query($params);
// 发起接口请求
$response = juheHttpRequest($apiUrl, $paramsString, 1);
// 处理接口返回结果,根据自身业务逻辑修改处理
$paramstring = http_build_query($params);
$content = juheHttpRequest($apiUrl, $paramstring, 1);
$result = json_decode($content, true);
if ($result) {
if ($result['error_code'] == 0) {
// 请求成功,根据自身业务逻辑修改处理
$news = $result['result']['data'];
if ($news) {
foreach ($news as $key => $newsInfo) {
// 更多字段,请参考官方接口文档
echo $newsInfo['title'].PHP_EOL;
}
}
} else {
// 请求异常,根据自身业务逻辑修改处理
echo "{$result['error_code']}:{$result['reason']}" . PHP_EOL;
}
} else {
//可能网络异常等问题请求失败,根据自身业务逻辑修改处理
echo "请求失败";
}
登录后复制
3、新闻头条详情查询
复制代码1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?php
/**
* 聚合新闻头条 - 新闻详情查询
* 功能:最新新闻头条,各类社会、国内、国际、体育、娱乐、科技等资讯。
*/
// 请求的接口URL
$apiUrl = 'http://v.juhe.cn/toutiao/content';
// 请求参数
$params = [
'uniquekey' => 'f9b3e37d91b452e182eda11db61e9c99', // 新闻ID
'key' => 'xxxxxx', // 接口调用key,通过聚合平台申请开通
];
$paramsString = http_build_query($params);
// 发起接口请求
$response = juheHttpRequest($apiUrl, $paramsString, 1);
// 处理接口返回结果,根据自身业务逻辑修改处理
$paramstring = http_build_query($params);
$content = juheHttpRequest($apiUrl, $paramstring, 1);
$result = json_decode($content, true);
if ($result) {
if ($result['error_code'] == 0) {
// 请求成功,根据自身业务逻辑修改处理
$newsContent = $result['result']['content'];
echo $newsContent;
} else {
// 请求异常,根据自身业务逻辑修改处理
echo "{$result['error_code']}:{$result['reason']}" . PHP_EOL;
}
} else {
//可能网络异常等问题请求失败,根据自身业务逻辑修改处理
echo "请求失败";
}
登录后复制
4、通用HTTP网络请求函数
复制代码1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/**
* 发起网络请求函数
* @param string $url 请求的URL
* @param bool $params 请求的参数内容
* @param int $ispost 是否POST请求
* @return bool|string 返回内容
*/
function juheHttpRequest($url, $params = false, $ispost = 0)
{
$httpInfo = [];
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_USERAGENT, 'JUHE API');
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
curl_setopt($ch, CURLOPT_TIMEOUT, 12);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if ($ispost) {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_URL, $url);
} else {
if ($params) {
curl_setopt($ch, CURLOPT_URL, $url . '?' . $params);
} else {
curl_setopt($ch, CURLOPT_URL, $url);
}
}
$response = curl_exec($ch);
if ($response === FALSE) {
// echo "cURL Error: ".curl_error($ch);
return false;
}
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$httpInfo = array_merge($httpInfo, curl_getinfo($ch));
curl_close($ch);
return $response;
}
登录后复制
推荐学习:《PHP视频教程》
以上就是php怎么查询新闻头条的详细内容,更多请关注靠谱客其它相关文章!
最后
以上就是时尚大雁最近收集整理的关于php怎么查询新闻头条的全部内容,更多相关php怎么查询新闻头条内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复