如何从Groupon API获取JSON数据

问题描述 投票:0回答:2

我正在努力从下面的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;  

}   
?>

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

非常感谢,

json api groupon
2个回答
0
投票

好的,这里是答案:

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

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;  
            }
        ?>
© www.soinside.com 2019 - 2024. All rights reserved.