简要说明 php利用curl函数请求新浪微博提供的热搜接口,返回json数据,利用php json_decode函数将json数据转换为php数组,利用php foreach函数将数组循环显示 cod…
简要说明
php利用curl函数请求新浪微博提供的热搜接口,返回json数据,利用php json_decode函数将json数据转换为php数组,利用php foreach函数将数组循环显示
code
<?php $weather = curl_init(); curl_setopt($weather,CURLOPT_URL,"https://api.oioweb.cn/api/summary.php"); curl_setopt($weather, CURLOPT_SSL_VERIFYPEER, false); //如果接口URL是https的,我们将其设为不验证,如果不是https的接口,这句可以不用加 curl_setopt($weather,CURLOPT_RETURNTRANSFER,true); $data = curl_exec($weather); curl_close($weather); $data=json_decode($data,true);//将json格式转化为数组格式,方便使用if(is_array($data)){ $i=0; foreach($data as $val){ if($i==9){ break; } echo "<a href=".$val['link'].">".$val['title']."</a>",'</br>'; $i++; }}else { echo "这不是一个数组";}