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