0

I am trying to write code to retrieve deals from the Groupon API and merge them by category in foreach loops but I can't merge them correctly files by files.

I want to obtain by example a file for each category (the first array). And if possible another which can concatenate all files into one global json file.

My code works but my json files are not well formatted I obtain results like this :

    "Categorie": "jewelry-and-watches"
}][{
    "id": "bracelet-bangle-feuille-cristaux-swarovski",

It's not valid cause I can't arrive to merge multiple pages correctly. So result is :

Error: Parse error on line 49475:
...elry-and-watches"}][{    "id": "bracelet-
----------------------^
Expecting 'EOF', '}', ',', ']', got '['

I can correct the result with string replace but it's a waste of time and I would like to have a nice code to understand the logic of this multiples foreachs ....

Here is my code :

<?php

set_time_limit(0); 
ini_set('memory_limit', '-1');

$baseurl="https://partner-int-api.groupon.com/deals.json?country_code=FR&tsToken=XXXXXX";
$baseurl_local="https://partner-int-api.groupon.com/division.json?country_code=FR";

$dir = '/var/www/vhosts/XXXXXXXXXX/import/catalogues/Groupon/';
$extension = ".json";

echo "
\n\n ////////////////////////////////////////////////////////
//GROUPON CATALOGUE FRANCE
//////////////////////////////////////////////////////// \n";

echo "
\n\n ////////////////////////////////////////////////////////
// Import des categories Goods
//////////////////////////////////////////////////////// \n";

$categories=array(
"auto-and-home-improvement",
"baby-kids-and-toys",
"electronics",
"entertainment-and-media",
"for-the-home",
"health-and-beauty",
"jewelry-and-watches",
"health-and-beauty",
"mens-clothing-shoes-and-accessories",
"sports-and-outdoors",
"womens-clothing-shoes-and-accessories");

foreach ($categories as $category) {
    $url_cata_test="$baseurl&filters=topcategory:goods&filters=category:$category&limit=1";
    $jsontest = file_get_contents($url_cata_test);
    $arrtest=json_decode($jsontest);
    $items=$arrtest->pagination->count;
    echo $items;
    $pagemax=ceil($items/98)+1;
    $pages = range(0,$pagemax);

    foreach ($pages as $page) {
        $url_cata="$baseurl&filters=topcategory:goods&filters=category:$category&offset=$page&limit=98";
        echo "Cat en cours d import: ".$category."\n";
        echo "Page en cours d import: ".$page."\n";
        echo "URL Cata: $url_cata \n";
        $urls = array($url_cata);

        foreach ($urls as $url){
            echo " \n //////////////////////////////////// \n";
            echo "Categorie en cours d import: ".$category."\n";
            echo "Nombre de produits: ".$items."\n";
            echo "Nombre de pages: ".$pagemax."\n";
            echo "Page en cours d import: ".$page."\n";
            echo "URL Cata: $url \n";
            echo " \n //////////////////////////////////// \n";

            $json = file_get_contents($url);
                $datas_decode = json_decode($json,TRUE);
            $deals = $datas_decode['deals'];
            foreach ($deals as $i => &$element) {$element['Categorie'] = $category;};
            $deals_encode = json_encode($deals);
            $deals_groupe = json_decode($deals_encode,TRUE);
            $allDataGoods = array();
            $alldeals = array_merge($allDataGoods,$deals_groupe);
            $datas_encode = json_encode($alldeals);
            file_put_contents($dir.$category.$extension, $datas_encode, FILE_APPEND);
        };
    };
};


?>
theduck
  • 2,589
  • 13
  • 17
  • 23
Sébastien
  • 39
  • 4
  • Your code is very difficult to read and understand, the one thing that may be your problem is that near the end you use `, FILE_APPEND);` with `file_put_contents()`. This most likely isn't correct and you just need to overwrite the current file. – Nigel Ren Nov 30 '19 at 09:59
  • @Nigel Ren : i use FILE_APPEND because Groupon doesn t provide a full list of deals for each categorie, i must scrape page after page to merge total category like electronics to obtain electronics.json ... I know it stupid to provide an API like that but i have no choice. – Sébastien Nov 30 '19 at 10:15
  • 1
    That is the problem, you need to read the data from the file and merge the data. At the moment you have a list of JSON strings in that file and not 1 JSON structure. – Nigel Ren Nov 30 '19 at 10:16
  • But when i do that i read no ?$json = file_get_contents($url); $datas_decode = json_decode($json,TRUE); – Sébastien Nov 30 '19 at 11:46

0 Answers0