doukeng7426 2016-08-22 18:00
浏览 189
已采纳

将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 2016-08-22 19:54
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路