題目描述
我想通過網(wǎng)站的json獲取信息,不知道為什么硬是獲取不了,json網(wǎng)址測試了,是正常的,大佬們幫我研究下代碼看下有什么問題嗎
題目來源及自己的思路
獲取數(shù)據(jù)好像是正常的,自己感覺問題好像出現(xiàn)在$timeline=json_decode($timeline,true);這里唉
相關代碼
<?php
function get_file_content($url,$fgc=false){
$file_contents='';
if($fgc&&function_exists('file_get_contents')){
$file_contents=@file_get_contents($url);
}
if(empty($file_contents)){
$ch=curl_init();
$timeout=5;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$file_contents=curl_exec($ch);
curl_close($ch);
}
return $file_contents;
}
$timeline=get_file_content('這個是json網(wǎng)址');
$timeline=json_decode($timeline,true);
if($timeline){
$title=$timeline['posts'][0]['title'];
echo $title;
}else{
echo(0);
}
?>
- 4 回答
- 0 關注
- 649 瀏覽
添加回答
舉報
0/150
提交
取消