Search code examples
datecommandsymfony4php-7.2

Is there a way to fix a date error using Symfony 4?


I'm working on a project as a back-end developer using PHP 7.2.11 and Symfony 4.2.3

My goal is to showcase events (concerts, festivals...) from data I grab from an API on an interactive map.

This is the result I get:

https://i.sstatic.net/ZOzh4.jpg

To import the data from the API, I use a for loop going from 1 to 7 (corresponding to days) and adding days to the today's date.

This date will be used as a parameter to grab the events occuring from today to the next 7 days.

Problem is: I get the following error running my custom symfony command php bin/console import:mapado :

2019-03-26T12:08:34+01:00 [error] Error thrown while running command "import:mapado". Message: "Notice: Undefined index: address"

The error is due to a date not existing and then refering to an inexistant address.

I tried to change the parameters in my first for loop, with days going either to 8 or to 6 but it didn't change the output error.

I change the custom date parameter (from my loop) from the API url to the default parameter and everything is working with default (but it only gets the events for the 3 next days).

Here is the parameter I use:

https://i.sstatic.net/6cFXC.png

From: https://api.mapado.net/v2/docs#operation/getActivityCollection

And how an element from the API looks like:

https://i.sstatic.net/Xz8Cc.png

This is the code I've written:

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

for better readability:

https://pastebin.com/CTu5gb8t

Expected result is a json output in my console. Actual result is an error thrown preventing me from inserting results into my database.

Could you tell me if I'm missing something that could result in this error ?

It's a long and detailed post so that you can understand better my problem.


Solution

  • Well I solved my problem and it was not a date problem.

    The problem was with the itemsPerPage query parameter requesting too much data and then throwing an error.

    I set it up to 400 and everything is working as expected.