我使用以下方法从 API 获取数据:function get_web_page($url) { $options = array( CURLOPT_RETURNTRANSFER => true, // return web page CURLOPT_HEADER => false, // don't return headers CURLOPT_FOLLOWLOCATION => true, // follow redirects CURLOPT_MAXREDIRS => 10, // stop after 10 redirects CURLOPT_ENCODING => "", // handle compressed CURLOPT_USERAGENT => "Space-X", // name of client CURLOPT_AUTOREFERER => true, // set referrer on redirect CURLOPT_CONNECTTIMEOUT => 120, // time-out on connect CURLOPT_TIMEOUT => 120, // time-out on response ); $ch = curl_init($url); curl_setopt_array($ch, $options); $content = curl_exec($ch); curl_close($ch); return $content;} $response = get_web_page("https://api.spacexdata.com/v3/launches/latest?pretty=true"); $resArr = array(); $resArr = json_decode($response); echo "<pre>"; print_r($resArr); echo "</pre>";使用返回的数据,我试图从数组中获取特定元素。例如,通过使用航班号很容易:$flight_number = $resArr->flight_number;echo $flight_number;但是执行以下操作来获取序列号不起作用。我哪里错了?$core_serial = $resArr->rocket->first_stage->cores->0->core_serial;echo $core_serial;
1 回答
慕容708150
TA贡献1831条经验 获得超4个赞
你有一个数组在那里,特别是 cores
看看它是如何在转储上注释的,[cores] => Array
而不是,例如[rocket] => stdClass Object
所以
$core_serial = $resArr->rocket->first_stage->cores[0]->core_serial;
- 1 回答
- 0 关注
- 129 浏览
添加回答
举报
0/150
提交
取消