我正在使用以下方法从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;
这是API的输出:
stdClass Object
(
[flight_number] => 78
[mission_name] => CRS-17
[mission_id] => Array
(
[0] => EE86F74
)
[launch_year] => 2019
[launch_date_unix] => 1556952480
[launch_date_utc] => 2019-05-04T06:48:00.000Z
[launch_date_local] => 2019-05-04T02:48:00-04:00
[is_tentative] =>
[tentative_max_precision] => hour
[tbd] =>
[launch_window] => 0
[rocket] => stdClass Object
(
[rocket_id] => falcon9
[rocket_name] => Falcon 9
[rocket_type] => FT
[first_stage] => stdClass Object
(
[cores] => Array
(
[0] => stdClass Object
(
[core_serial] => B1056
[flight] => 0
[block] => 5
[gridfins] => 1
[legs] => 1
[reused] =>
[land_success] => 1
[landing_intent] => 1
[landing_type] => ASDS
[landing_vehicle] => OCISLY
)
)
)
[second_stage] => stdClass Object
(
[block] => 5
[payloads] => Array
(
[0] => stdClass Object
(
[payload_id] => CRS-17
[norad_id] => Array
(
[0] => 44222
)
[cap_serial] => C113
[reused] => 1
[customers] => Array
(
[0] => NASA (CRS)
)
[nationality] => United States
[manufacturer] => SpaceX
[payload_type] => Dragon 1.1
[payload_mass_kg] => 2482
[payload_mass_lbs] => 5472
[orbit] => ISS
[orbit_params] => stdClass Object
(
[reference_system] => geocentric
[regime] => low-earth
[longitude] =>
[semi_major_axis_km] => 6787.07
[eccentricity] => 0.0001369
[periapsis_km] => 408.006
[apoapsis_km] => 409.865
[inclination_deg] => 51.6417
[period_min] => 92.743
[lifespan_years] =>
[epoch] => 2019-05-14T13:40:22.000Z
[mean_motion] => 15.5267006
[raan] => 168.2377
[arg_of_pericenter] => 352.6849
[mean_anomaly] => 127.392
)
[mass_returned_kg] =>
[mass_returned_lbs] =>
[flight_time_sec] =>
[cargo_manifest] => https://www.nasa.gov/sites/default/files/atoms/files/spacex_crs-17_mission_overview.pdf
)
)
)
[fairings] =>
)
[ships] => Array
(
[0] => OCISLY
[1] => Hollywood
[2] => GOQUEST
)
[telemetry] => stdClass Object
(
[flight_club] => https://www.flightclub.io/result/2d?code=CR17
)
[launch_site] => stdClass Object
(
[site_id] => ccafs_slc_40
[site_name] => CCAFS SLC 40
[site_name_long] => Cape Canaveral Air Force Station Space Launch Complex 40
)
[launch_success] => 1
[links] => stdClass Object
(
[mission_patch] => https://images2.imgbox.com/12/47/6uim8L1a_o.png
[mission_patch_small] => https://images2.imgbox.com/fc/58/9UErD3ut_o.png
[reddit_campaign] => https://new.reddit.com/r/spacex/comments/bd2l28/crs17_launch_campaign_thread/
[reddit_launch] => https://www.reddit.com/r/spacex/comments/bjsn0v/rspacex_crs17_official_launch_discussion_updates
[reddit_recovery] => https://www.reddit.com/r/spacex/comments/bjy7p5/rspacex_crs17_recovery_discussion_updates_thread
[reddit_media] => https://www.reddit.com/r/spacex/comments/bkc4d5/rspacex_crs17_media_thread_videos_images_gifs
[presskit] => https://www.spacex.com/sites/spacex/files/crs-17_press_kit.pdf
[article_link] => https://spaceflightnow.com/2019/05/04/spacex-launches-space-station-resupply-mission-lands-rocket-on-drone-ship/
[wikipedia] => https://en.wikipedia.org/wiki/SpaceX_CRS-17
[video_link] => https://youtu.be/AQFhX5TvP0M
[youtube_id] => AQFhX5TvP0M
[flickr_images] => Array
(
[0] => https://live.staticflickr.com/65535/46856594435_206c773b5a_o.jpg
[1] => https://live.staticflickr.com/65535/47720639872_284e49381d_o.jpg
[2] => https://live.staticflickr.com/65535/46856594755_88f1b22e50_o.jpg
[3] => https://live.staticflickr.com/65535/47720639542_1b7c1a71b0_o.jpg
[4] => https://live.staticflickr.com/65535/47720639732_e04b2a9ed7_o.jpg
[5] => https://live.staticflickr.com/65535/32829382467_087d024428_o.jpg
)
)
[details] => SpaceX's 17th Commercial Resupply Services mission for NASA out of a total of 20 contracted flights, this mission brings essential supplies to the International Space Station using SpaceX's reusable Dragon 1 spacecraft. The external payloads for this mission include Orbital Carbon Observatory 3 and Space Test Program-Houston 6. The Falcon 9 launches from SLC-40 at Cape Canaveral AFS. The booster was expected to land at LZ-1, however, due to the ongoing investigation and clean-up following the Crew Dragon testing incident, it is likely to land on OCISLY instead.
[upcoming] =>
[static_fire_date_utc] => 2019-04-27T07:23:00.000Z
[static_fire_date_unix] => 1556349780
[timeline] => stdClass Object
(
[webcast_liftoff] => 900
[go_for_prop_loading] => -2280
[rp1_loading] => -2100
[stage1_lox_loading] => -2100
[stage2_lox_loading] => -960
[engine_chill] => -420
[prelaunch_checks] => -60
[propellant_pressurization] => -60
[go_for_launch] => -45
[ignition] => -3
[liftoff] => 0
[maxq] => 72
[meco] => 137
[stage_sep] => 141
[second_stage_ignition] => 148
[first_stage_boostback_burn] => 154
[first_stage_entry_burn] => 399
[first_stage_landing] => 507
[seco-1] => 519
[dragon_separation] => 578
[dragon_solar_deploy] => 728
[dragon_bay_door_deploy] => 8340
)
)
答案 0 :(得分:3)
您在混合中有一个数组,特别是cores
查看转储[cores] => Array
而不是[rocket] => stdClass Object
上的注释方式
如此
$core_serial = $resArr->rocket->first_stage->cores[0]->core_serial;