doukeng7426
doukeng7426
2016-08-22 18:00

将Curl参数作为参数传递

已采纳

I have a curl function that requests data on Asteroids and get backs results. Currently I am just hard coding in the date parameters for the result, I would like the user to be able to enter their selected dates and get the result how could I add this, I am new to using Curl and keep hitting a wall, the code so far is below,

<?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>";
}
?>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • dongqiao3833 dongqiao3833 5年前

    You were close. I modified your code slightly. The date format you had was also incorrect. You were providing the format mm/dd/yyyy when the API calls for a format of yyyy-mm-dd (notice the year goes in front and you need hyphens instead of slashes).

    I used GET values for the start and end date (note the API only allows 7 days between the start and end dates). So you can change the date by adding ?start=2015-05-01&end=2015-05-05 to the end of the URL you use to access this code (it could also be implemented as a GET-method form):

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

    The dates can be entered in any format that strtotime understands. In the event of an invalid date it will default to todays date. If the API throws an error (like when the date range is larger than 7 days), it will just throw an error. You may want to add some error detection.

    点赞 评论 复制链接分享