我使用以下方法从 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;
慕容708150