【问题标题】:Is there a way to fix a date error using Symfony 4?有没有办法使用 Symfony 4 修复日期错误?
【发布时间】:2019-08-16 22:16:48
【问题描述】:

我正在使用 PHP 7.2.11 和 Symfony 4.2.3 作为后端开发人员开发一个项目

我的目标是根据我从交互式地图上的 API 获取的数据展示活动(音乐会、节日......)。

这是我得到的结果:

https://imgur.com/HmpBK5B.png

为了从 API 导入数据,我使用了一个 for 循环,从 1 到 7(对应于天)并将天添加到今天的日期。

此日期将用作参数,以获取从今天到未来 7 天发生的事件。

问题是:运行自定义 symfony 命令时出现以下错误 php bin/console import:mapado :

2019-03-26T12:08:34+01:00 [错误] 运行命令“import:mapado”时引发错误。消息:“注意:未定义的索引:地址”

错误是由于日期不存在,然后引用了不存在的地址。

我尝试在我的第一个 for 循环中更改参数,将天数变为 8 或 6,但它并没有改变输出错误。

我将自定义日期参数(来自我的循环)从 API url 更改为默认参数,一切都使用默认参数(但它只获取接下来 3 天的事件)。

这是我使用的参数:

https://imgur.com/tx1OyrM.png

发件人:https://api.mapado.net/v2/docs#operation/getActivityCollection

以及来自 API 的元素的外观:

https://imgur.com/l1nTOCC.png

这是我写的代码:

protected function execute(InputInterface $input, OutputInterface $output) {
        for ($jour = 0; $jour <= 7; $jour++) {
           // problem is here
            $futureDay = date('Y-m-d', strtotime('+'.$jour.' days'));

            $curl = curl_init();

            curl_setopt_array($curl, array(
                CURLOPT_URL => "https://api.mapado.net/v2/activities?fields=@id,title,shortDate,nextDate,activityType,locale,description,address&itemsPerPage=1000&when=".$futureDay."&periodOfDay=evening",
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_ENCODING => "",
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_TIMEOUT => 30,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => "GET",
                CURLOPT_HTTPHEADER => array(
                    "Authorization: Bearer MTMwZWJiODFiZjA4YTcyOGY2ZmMzMGYwOTQyYWM2NDZjODVlNDg1MzU0MzE3M2I4MTdiMDQyZjU5MDVkZjFjZA",
                    "Cache-Control: no-cache",
                    "Conent-Type: application/json",
                    "Content-Type: application/x-www-form-urlencoded",
                    "Postman-Token: 55672a19-0ffc-4fe6-a866-3e15c3df9dae"
                ),
            ));

            $response = curl_exec($curl);
            $err = curl_error($curl);

            $mapado_events = json_decode($response, JSON_PRETTY_PRINT);

            for ($i = 0; $i < count($mapado_events['hydra:member']); $i++) {

                if ($mapado_events['hydra:member'][$i]['locale'] == 'fr') {

                    $mapado_id = $mapado_events['hydra:member'][$i]['@id'];
                    $mapado_date = \date('Y-m-d', strtotime($mapado_events['hydra:member'][$i]['nextDate']));

                    $result = $this->getContainer()
                        ->get('doctrine')
                        ->getRepository(MapadoIDs::class)
                        ->findOneBy(['mapado_id' => $mapado_id]);

                    if ($result == null) {
                        echo 'event existe pas, ajout en bdd'.PHP_EOL;
                        $MapadoIDs = new MapadoIDs();
                        $MapadoIDs->setMapadoId($mapado_id);
                        $this->em->persist($MapadoIDs);

                        $mapado = json_decode($response, JSON_PRETTY_PRINT);

                        $event = new Event();

                        $event->setLongitude($mapado['hydra:member'][$i]['address']['longitude']);
                        $event->setLatitude($mapado['hydra:member'][$i]['address']['latitude']);
                        $event->setTitle($mapado['hydra:member'][$i]['title']);
                        $event->setDate($mapado_date);
                        $event->setFormattedAddress($mapado['hydra:member'][$i]['address']['formattedAddress']);
                        $event->setCity($mapado['hydra:member'][$i]['address']['city']);
                        $event->setLocale($mapado['hydra:member'][$i]['locale']);
                        $event->setActivityType($mapado['hydra:member'][$i]['activityType']);
                        $event->setDescription($mapado['hydra:member'][$i]['description']);

                        $this->em->persist($event);                     
                    }
                }
            }
        }

        $this->em->flush();

        curl_close($curl);

        if ($err) {
            echo "cURL Error #: " . $err;
        } else {
            echo $response;
        }
    }
}

为了更好的可读性:

https://pastebin.com/CTu5gb8t

预期结果是我的控制台中的 json 输出。 实际结果是一个错误,阻止我将结果插入到我的数据库中。

能否告诉我是否遗漏了可能导致此错误的内容?

这是一篇长而详细的帖子,以便您更好地理解我的问题。

【问题讨论】:

    标签: date command symfony4 php-7.2


    【解决方案1】:

    好吧,我解决了我的问题,这不是日期问题。

    问题在于 itemsPerPage 查询参数请求的数据过多,然后引发错误。

    我将它设置为 400,一切都按预期工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-04
      • 2019-10-10
      • 1970-01-01
      • 1970-01-01
      • 2021-07-01
      相关资源
      最近更新 更多