2012-10-12 135 views
0

我試圖將一個Amazon API響應對象保存到名爲items.json的文件中 響應本身在json中,但是當我使用這個函數時,我得到的文件是空的。 不介意unlink()函數這是一個cron作業。如何將亞馬遜API JSON響應保存到JSON文件?

<?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://rads.stackoverflow.com/amzn/click/B004YDSL9Q" 
     ["ItemLinks"]=> 
     object(stdClass)#16 (1) { 
      ["ItemLink"]=> 
      array(7) { 
      [0]=> 
      object(stdClass)#17 (2) { 
       ["Description"]=> 
       string(17) "Technical Details" 
       ["URL"]=> 
       string(218) "http://rads.stackoverflow.com/amzn/click/B004YDSL9Q" 
      } 
      [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://rads.stackoverflow.com/amzn/click/B004YDSL9Q" 
      } 
      [5]=> 
      object(stdClass)#22 (2) { 
       ["Description"]=> 
       string(20) "All Customer Reviews" 
       ["URL"]=> 
       string(188) "http://rads.stackoverflow.com/amzn/click/B004YDSL9Q" 
      } 
      [6]=> 
      object(stdClass)#23 (2) { 
       ["Description"]=> 
       string(10) "All Offers" 
       ["URL"]=> 
       string(190) "http://rads.stackoverflow.com/amzn/click/B004YDSL9Q" 
      } 
      } 
     } 

以及其他項目。

我錯了:API響應不是 JSON,它是一個對象,你必須在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

檢查'file_put_contents'的返回值 – Matthew

+0

它返回bool(false)... – Haldir87

+1

這意味着它失敗(http://php.net/file_put_contents)。我認爲這可能是權限.. – Matthew

回答

1

如果您創建的文件是空的,那麼$response變種是空的。做一些調試。 var_dump的反應,看看你得到什麼。如果文件顯示在應該顯示的位置,但是顯示爲空,那麼這取決於您試圖在其中存儲的內容,其中的情況爲$response

所以做到這一點:

var_dump($response); 

然後再回到這裏,告訴我們你所看到的。 PS:我會張貼這個評論(因爲我知道這不是一個答案),但我沒有代表。