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.

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

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题