如何将Amazon API JSON响应保存到JSON文件?

时间:2012-10-12 11:06:28

标签: php json amazon

我正在尝试将Amazon API响应对象保存到名为items.json的文件中 响应本身是在json中,但是当我使用这个函数时,我得到的文件是空的。 不要介意unlink()函数这是一个cron作业。

<?php
/**
 * Amazon ECS library
 */

if ("cli" !== PHP_SAPI)
{
    echo "<pre>";
}

if (is_file('sampleSettings.php'))
{
  include 'sampleSettings.php';
}

defined('AWS_API_KEY') or define('AWS_API_KEY', 'API KEY');
defined('AWS_API_SECRET_KEY') or define('AWS_API_SECRET_KEY', 'SECRET KEY');
defined('AWS_ASSOCIATE_TAG') or define('AWS_ASSOCIATE_TAG', 'ASSOCIATE TAG');

require '../lib/AmazonECS.class.php';

try
{

    $amazonEcs = new AmazonECS('AWS_API_KEY', 'AWS_API_SECRET_KEY', 'com', 'AWS_ASSOCIATE_TAG');

   $amazonEcs->associateTag(AWS_ASSOCIATE_TAG);

   $response = $amazonEcs->category('KindleStore')->responseGroup('Small,Images')->search('free ebooks');

    //Check if the json file exist
   $filename = "/home/myusername/public_html/my/path_to/items.json";
   if(file_exists($filename)){
    //delete it
    unlink($filename);
    file_put_contents("items.json", $response);
   }else{
    file_put_contents("items.json", $response);
   }    
}
catch(Exception $e)
{
  echo $e->getMessage();
}


if ("cli" !== PHP_SAPI)
{
    echo "</pre>";
}

有关如何解决此问题的任何建议吗?

编辑:$ response变量不为空;这是我在var_dump时得到的结果:

  ["Item"]=>
    array(10) {
      [0]=>
      object(stdClass)#15 (8) {
        ["ASIN"]=>
        string(10) "B004YDSL9Q"
        ["DetailPageURL"]=>
        string(208) "http://www.amazon.com/The-Love-suspense-mystery-ebook/dp/B004YDSL9Q%3F"
        ["ItemLinks"]=>
        object(stdClass)#16 (1) {
          ["ItemLink"]=>
          array(7) {
            [0]=>
            object(stdClass)#17 (2) {
              ["Description"]=>
              string(17) "Technical Details"
              ["URL"]=>
              string(218) "http://www.amazon.com/The-Love-suspense-mystery-ebook/dp/tech-data/B004YDSL9Q%3F"
            }
            [1]=>
            object(stdClass)#18 (2) {
              ["Description"]=>
              string(20) "Add To Baby Registry"
              ["URL"]=>
              string(215) "http://www.amazon.com/gp/registry/baby/add-item.html"
            }
            [2]=>
            object(stdClass)#19 (2) {
              ["Description"]=>
              string(23) "Add To Wedding Registry"
              ["URL"]=>
              string(218) "http://www.amazon.com/gp/registry/wedding/add-item.html%3Fasin.0%3DB004YDSL9Q%26"
            }
            [3]=>
            object(stdClass)#20 (2) {
              ["Description"]=>
              string(15) "Add To Wishlist"
              ["URL"]=>
              string(219) "http://www.amazon.com/gp/registry/wishlist/add-item.html%3Fasin.0%3DB004YDSL9Q%26"
            }
            [4]=>
            object(stdClass)#21 (2) {
              ["Description"]=>
              string(13) "Tell A Friend"
              ["URL"]=>
              string(184) "http://www.amazon.com/gp/pdp/taf/B004YDSL9Q%3F"
            }
            [5]=>
            object(stdClass)#22 (2) {
              ["Description"]=>
              string(20) "All Customer Reviews"
              ["URL"]=>
              string(188) "http://www.amazon.com/review/product/B004YDSL9Q%3FSubscriptionId%3"
            }
            [6]=>
            object(stdClass)#23 (2) {
              ["Description"]=>
              string(10) "All Offers"
              ["URL"]=>
              string(190) "http://www.amazon.com/gp/offer-listing/B004YDSL9Q%3"
            }
          }
        }

以及其他项目。

我错了:API响应不是 JSON,它是一个Object,你必须在json中对它进行编码。

这就是我实际解决的问题:

-cut-

   $response = $amazonEcs->category('KindleStore')->responseGroup('Small,Images')->search('free ebooks');
//json_encode on $response because the response of the API is not JSON, but it is an object

$data = json_encode($response);

//Using stripslashes function to delete all the slashed that has been added to $data by the encoding process

$data2 = stripslashes($data);

file_put_contents("items.json", $data2);

1 个答案:

答案 0 :(得分:1)

如果您创建的文件为空,则$response var为空。做一些调试。 var_dump回复,看看你得到了什么。如果文件显示在应该显示的位置,但是显示为空,那么这取决于您尝试存储在其中的内容,在此情况下$response

这样做:

var_dump( $response );

然后回到这里告诉我们你的看法。

PS:我会将此作为评论发布(因为我知道这不是一个答案),但我没有代表。