我有一个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 :(得分:1)
你很亲密。我稍微修改了你的代码。您的日期格式也不正确。当API调用public void addRow (string[] values) {
// Validate public methods arguments
if (null == values)
throw new ArgumentNullException("values");
// Make the query parametrized: it's faster and not prone to SQL injection
// Do not append String in a loop; use Join instead
string sql = string.Format("INSERT INTO {0} VALUES ({1})",
tableName,
string.Join(", ", values.Select((item, index) => "@param_" + index.ToString()));
// wrap IDisposable into using
// let createConnection() return the connection created
using (var con = createConnection()) {
con.Open();
// wrap IDisposable into using
using (var cmd = new SqlCommand(sql, con)) {
// Parametrized query: parameters assigning
for (int index = 0; index < values.Length; ++index)
cmd.Parameters.AddWithValue("@param_" + index.ToString(), values[i]);
// Finally query executing
cmd.ExecuteNonQuery();
}
}
}
#include <tuple>
template <typename Class, typename T>
struct Property {
constexpr Property(T Class::* const member) : m_member{ member } {}
T Class::* const m_member;
};
template <class T, class V>
constexpr Property<T, V> get_age_property() {
return Property<T, V>(&T::age);
}
class User
{
public:
int age;
constexpr static std::tuple<Property<User, int>> properties = std::make_tuple(
get_age_property<User, int>()
);
};
int main()
{
}
时,您提供的格式为mm/dd/yyyy
(请注意年份在前面,您需要连字符而不是斜线)。
我在开始日期和结束日期使用了GET值(请注意,API仅在开始日期和结束日期之间允许format)。因此,您可以通过将yyyy-mm-dd
添加到用于访问此代码的URL的末尾来更改日期(它也可以实现为GET方法表单):
?start=2015-05-01&end=2015-05-05
可以用strtotime理解的任何格式输入日期。如果日期无效,则默认为今天的日期。如果API抛出错误(比如日期范围大于7天),它只会抛出错误。您可能想要添加一些错误检测。