2016-08-22 252 views
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>"; 
} 
?> 

回答

0

你就近了。我稍微修改了你的代碼。你的日期格式也是不正確的。當API要求formatyyyy-mm-dd(注意年份在前面並且您需要連字符而不是斜線)時,您正在提供格式mm/dd/yyyy

我使用GET值作爲開始日期和結束日期(請注意API僅允許在起始日期和結束日期之間使用7 days)。所以,你可以通過添加?start=2015-05-01&end=2015-05-05到URL的末尾,你用它來訪問該代碼(這也可以被實現爲GET-方法形式)更改日期:

<?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; 
} 

$startDate = date('Y-m-d', strtotime(isset($_GET['start']) ? $_GET['start'] : date('Y-m-d'))); 
$endDate = date('Y-m-d', strtotime(isset($_GET['end']) ? $_GET['end'] : date('Y-m-d'))); 

if(!$startDate) { 
    $startDate = date('Y-m-d'); 
} 

if(!$endDate) { 
    $endDate = date('Y-m-d'); 
} 

$params = array(
    'start_date' => $startDate, 
    'end_date' => $endDate, 
    'api_key' => 'NNKOjkoul8n1CH18TWA9gwngW1s1SmjESPjNoUFo' 
); 

$data = json_decode(callAPI('GET', 'https://api.nasa.gov/neo/rest/v1/feed', $params)); 

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>"; 
} 

的日期可以在任何格式輸入該strtotime理解。如果日期無效,它將默認爲今天的日期。如果API拋出一個錯誤(例如日期範圍大於7天),它只會拋出一個錯誤。您可能想要添加一些錯誤檢測。

+0

HI Jim感謝您的幫助,確實卡住了它,如果我希望用戶能夠在頁面上輸入日期,GET方法就像這樣

\t \t \t \t \t \t \t \t

+0

輸入'name =「startDate」'將會是'name =「start」',除非您更改代碼以使用startDate代替。 – Jim

+0

HI吉姆我不知道你能幫我進一步,我已經添加了一個功能,允許用戶輸入開始日期,我添加了代碼設置結束日期後7天,但頁面將加載之前,用戶甚至添加日期你幫忙。 –