将Curl参数作为参数传递(passing Curl parameter as a parameter)

编程入门 行业动态 更新时间:2024-10-11 01:11:44
将Curl参数作为参数传递(passing Curl parameter as a parameter)

我有一个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>"; } ?>

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

最满意答案

你很近。 我稍微修改了你的代码。 您的日期格式也不正确。 当API调用格式为yyyy-mm-dd时,您提供的格式为mm/dd/yyyy (注意年份在前面,您需要连字符而不是斜线)。

我在开始日期和结束日期使用了GET值(请注意,API仅允许在开始日期和结束日期之间有7天 )。 因此,您可以通过将?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天),它只会抛出错误。 您可能想要添加一些错误检测。

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.

更多推荐

本文发布于:2023-08-04 19:15:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1419180.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:参数   Curl   parameter   passing

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!