如何从Groupon API获取JSON数据

时间:2017-04-29 21:25:44

标签: json api groupon

我正在努力争取“商人” - >来自Groupon API的“id”,虽然我没有任何问题可以返回discountPercent。

<?php

$url = 'https://partner-int-api.groupon.com/deals.json?country_code=IE&tsToken=IE_AFF_0_200012_212556_0&division_id=dublin&offset=0&limit=10';
$content = file_get_contents($url);
$json = json_decode($content, true);

foreach($json['deals'] as $results) {

$discountPercent = $results['options'][0]['discountPercent'];
$merchantId = $results['merchant'][0]->id;

    echo $discountPercent.'<br>'; 
    echo $merchantId;  

}   
?>

如果有人能指出我正确的方向。

非常感谢,

2 个答案:

答案 0 :(得分:0)

好的,答案是:

$merchantId = $results['merchant']['id'];

答案 1 :(得分:0)

>>>This may be the Reason.....

>>>Explanation for::  $results['options'][0]['discountPercent']  
In $json array you can see
[options] => Array  
 (  
  [0] => Array  
   (  
    .........  
    [discountPercent] => 54    
     .........     
    )  
 )   
Here, the hierechy is  
option->0(key)->discountPercent  
that's why you need to use index '0'; 

 >>>Explanation for:::  $results['merchant'][0]->id  
in json array you can see  
[merchant] => Array  
 (  
  [id] => global-cuisine-restaurant  
  [uuid] => 183dd76b-a1a6-40cf-93c7-33e00f379451  
  [name] => Global Cuisine Restaurant  
  [websiteUrl] => http://www.globalcuisine.ie/  
  [twitterUrl] =>   
  [facebookUrl] =>   
  [ratings] =>   
 ) 
Here the hirerchy is:::  
merchant->id (notice '0' is not present as any subarray index)   
that's why should use  
 $merchantId = $results['merchant']['id']; 

 finally use can use code::  

        <?php 
            $url = 'https://partner-int-api.groupon.com/deals.json?country_code=IE&tsToken=IE_AFF_0_200012_212556_0&division_id=dublin&offset=0&limit=10';
            $content = file_get_contents($url);
            $json = json_decode($content, true);

            foreach($json['deals'] as $key=>$results) {
                $discountPercent = $results['options'][0]['discountPercent'];  
                $discountPercent[] = $results['options'][0]['discountPercent'];//to get all in one;   
                $merchantId = $results['merchant']['id'];  
                $merchantId[] = $results['merchant']['id'];//to get all in one  
                    echo $discountPercent.'<br>'; 
                    echo $merchantId;  
            }
        ?>