我在聚合数据网站上申请了几个免费的API接口来玩,然后研究了一下用Thinkphp5.0调用数据的方法,目前我能用这个拿到数据,只是在控制器里面添加了一个方法,条件都是手动添加的。当然也可以用变量来动态添加条件,根据接口情况而定。
namespace app\index\controller;
use think\Controller;
class Index extends Controller
{
public function news(){
$appkey = "db05e1234f593a7b458b43c3bfb2a000";
//************1.头条新闻信息查询************
$url = "http://v.juhe.cn/toutiao/index";
$params = [
"type" => "top",//类型
"dtype" => "json",//返回数据格式:json或xml,默认json
"key" => $appkey,//你申请的key
];
$paramstring = http_build_query($params); /*生成 URL-encode 之后的请求字符串*/
// print_r($paramstring);
// echo "
";
$ch = curl_init($url.'?'.$paramstring);
$contents = curl_exec($ch);
$title=$contents['title'];
$content=[
'title'=>$title,
];
return $content;
// return 1;
// curl_close($ch);
// print_r($content);
// echo "
";
// $result = json_decode($content,true);
// if($result){
// if($result['error_code']=='0'){
// print_r($result);
// }else{
// echo $result['error_code'].":".$result['reason'];
// }
// }else{
// echo "请求失败";
// }
//**************************************************
}
}
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号