2012-09-08 190 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); 

这将返回一个关联数组。