0
我有一個curl函數,用於請求有關Asteroids的數據並獲取返回的結果。目前我只是在結果的日期參數中編寫硬編碼,我希望用戶能夠輸入他們選擇的日期並獲得結果,我怎麼可以添加這個,我是使用Curl並保持撞牆的新手,代碼到目前爲止,將Curl參數作爲參數傳遞
<?php
session_start();
// Method: POST, PUT, GET etc
// Data: array("param" => "value") ==> index.php?param=value
function CallAPI($method, $url, $data = false)
{
$curl = curl_init();
switch ($method) {
case "POST":
curl_setopt($curl, CURLOPT_POST, 1);
if ($data)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
case "PUT":
curl_setopt($curl, CURLOPT_PUT, 1);
break;
default:
if ($data)
$url = sprintf("%s?%s", $url, http_build_query($data));
}
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($curl);
if ($result === FALSE) {
die(curl_error($curl));
}
curl_close($curl);
return $result;
}
$params = array(
'start_date' => '01/03/2016',
'end_date' => '02/03/2016',
'api_key' => 'demo key'
);
$data = json_decode(callAPI('GET', 'https://api.nasa.gov/neo/rest/v1/feed', $params));
//var_dump($data);
echo "<h1>Near-Earth Object (NEO) Report between " . $params['start_date'] . " and " . $params['end_date'] . "</h1>";
foreach ($data->near_earth_objects as $date => $count) {
echo "<p>" . sizeof($count) . " objects detected on $date</p>";
echo "<ol>";
foreach ($data->near_earth_objects->$date as $near_earth_object) {
echo "<li>" . $near_earth_object->name . " <a href='" . $near_earth_object->nasa_jpl_url . "'>" . $near_earth_object->nasa_jpl_url . "</a><br>";
echo "Estimated Diameter: " . $near_earth_object->estimated_diameter->meters->estimated_diameter_min . "-" . $near_earth_object->estimated_diameter->meters->estimated_diameter_max . " metres<br>";
echo "<ul>";
foreach ($near_earth_object->close_approach_data as $close_approach) {
echo "<li>Close approach on " . $close_approach->close_approach_date . " velocity " . $close_approach->relative_velocity->kilometers_per_hour . " km/h " . "missing " . $close_approach->orbiting_body . " by " . $close_approach->miss_distance->kilometers . " km</li> ";
}
echo "</ul></li>";
}
echo "</ol>";
}
?>
HI Jim感謝您的幫助,確實卡住了它,如果我希望用戶能夠在頁面上輸入日期,GET方法就像這樣
–輸入'name =「startDate」'將會是'name =「start」',除非您更改代碼以使用startDate代替。 – Jim
HI吉姆我不知道你能幫我進一步,我已經添加了一個功能,允許用戶輸入開始日期,我添加了代碼設置結束日期後7天,但頁面將加載之前,用戶甚至添加日期你幫忙。 –