2012-09-08 200 views
1

我試圖使用PHP和cURL調用eBay的Shopping API並以JSON格式返回響應。它可以直接在瀏覽器中使用,但它不在PHP中。我不想使用XML。 JSON更容易。有什麼建議麼?PHP cURL JSON eBay API請求

$Url ="http://open.api.ebay.com/shopping?callname=GetMultipleItems&responseencoding=JSON&appid=MyAppId&siteid=0&version=525&ItemID=290585620456,290683575886&IncludeSelector=Details,ShippingCosts,Variations"; 

//check if you have curl loaded 
if(!function_exists("curl_init")) die("cURL extension is not installed"); 

$ch=curl_init($Url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
$r=curl_exec($ch); 
curl_close($ch); 

var_dump($r); 
+0

什麼不行?你沒有收到任何數據嗎?錯誤的數據?空白頁面? – andrewsi

+1

您的代碼顯示'string(285)「{」Timestamp「:」2012-09-08T18:21:45.632Z「,」Ack「:」Failure「,」Errors「:[{」ShortMessage「:」Application ID invalid 「,」LongMessage「:」應用程序ID無效「,」ErrorCode「:」1.20「,」SeverityCode「:」錯誤「,」ErrorClassification「:」RequestError「}],」Build「:」E789_CORE_BUNDLED_15285085_R1「 「:」789「}」'對我來說。這看起來像json:o –

+0

嗯......這很奇怪。它現在有效。我不敢相信我花了一個小時。不管怎樣,謝謝! – user366810

回答

0

你應該使用

json_encode($response_string); 

這將解析響應字符串轉換成JSON對象。如果你想基本的數組,而不是JSON對象

,然後用

json_decode($responce_str, true); 

這將返回一個關聯數組。