PHP eBay呼叫问题

时间:2014-01-21 23:32:57

标签: php xml

我试图获取产品的送货信息,这是我的代码(我隐藏了我的app-id)。

 $endpoint2 = "http://open.api.ebay.com/shopping";

$xmlrequest2  = "<?xml version='1.0' encoding='utf-8'?>\n";
$xmlrequest2 .= "<GetShippingCostsRequest xmlns='urn:ebay:apis:eBLBaseComponents'>\n";
//  $xmlrequest2 .= "<DestinationCountryCode>GB</DestinationCountryCode>\n";
// $xmlrequest2 .= "<IncludeDetails>true</IncludeDetails>\n";
$xmlrequest2 .= "<ItemID>".$id."</ItemID>\n";
$xmlrequest2 .= "</GetShippingCostsRequest>\n";

$session2  = curl_init($endpoint2);                       // create a curl session
curl_setopt($session2, CURLOPT_POST, true);
var_dump($xmlrequest2);
curl_setopt($session2, CURLOPT_POSTFIELDS, $xmlrequest2); // set the body of the POST
curl_setopt($session2, CURLOPT_RETURNTRANSFER, true);
$headers2 = array(
'X-EBAY-API-APP-ID:'.getsetting(2),
 'X-EBAY-API-VERSION:849',
 'X-EBAY-API-SITE-ID:3',
 'X-EBAY-API-CALL-NAME:GetShippingCosts',
 'X-EBAY-API-REQUEST-ENCODING:XML',
);
print_r($headers2);

       // create a curl session
curl_setopt($session2, CURLOPT_HTTPHEADER, $headers2);    //set headers using the above array of headers
$responseXML = curl_exec($session2);
// send the request
//echo $responseXML;
curl_close($session2);

return $responseXML;

这是我得到的输出/错误。

    string(162) "<?xml version='1.0' encoding='utf-8'?>
<GetShippingCostsRequest xmlns='urn:ebay:apis:eBLBaseComponents'>
<ItemID>300903657321</ItemID>
</GetShippingCostsRequest>
"
Array
(
    [0] => X-EBAY-API-APP-ID:1234
    [1] => X-EBAY-API-VERSION:849
    [2] => X-EBAY-API-SITE-ID:3
    [3] => X-EBAY-API-CALL-NAME:GetShippingCosts
    [4] => X-EBAY-API-REQUEST-ENCODING:XML
)
<?xml version="1.0" encoding="UTF-8"?>

  <GetShippingCostsResponse xmlns="">
   <ns1:Ack xmlns:ns1="urn:ebay:apis:eBLBaseComponents">Failure</ns1:Ack>
   <ns2:Errors xmlns:ns2="urn:ebay:apis:eBLBaseComponents">
    <ns2:ShortMessage>Input data is invalid.</ns2:ShortMessage>
    <ns2:LongMessage>Input data for the given tag is invalid or missing. Please check API documentation.</ns2:LongMessage>
    <ns2:ErrorCode>1.22</ns2:ErrorCode>
    <ns2:SeverityCode>Error</ns2:SeverityCode>
    <ns2:ErrorParameters ParamID="0">
     <ns2:Value>XML document structures must start and end within the same entity.</ns2:Value>
    </ns2:ErrorParameters>
    <ns2:ErrorClassification>RequestError</ns2:ErrorClassification>
   </ns2:Errors>
   <ns3:Build xmlns:ns3="urn:ebay:apis:eBLBaseComponents">E853_CORE_APILW_16579549_R1</ns3:Build>
   <ns4:Version xmlns:ns4="urn:ebay:apis:eBLBaseComponents">853</ns4:Version>
  </GetShippingCostsResponse>

有什么想法吗?我无法解决问题所在。他们的文档中没有任何内容似乎有帮助。如果我将XML复制并粘贴到他们的API测试工具中,它就不会有任何问题。

2 个答案:

答案 0 :(得分:2)

每次调用eBay Shopping API都会遇到同样的问题。您可以修复此问题,将Content-Type: text/xml;charset=UTF-8添加到标题中(这可以解决我的问题)。

答案 1 :(得分:0)

我没有在任何地方看到您的凭据。他们为您提供的API工具有点具有欺骗性,因为它使用凭据包装您的呼叫。 http://developer.ebay.com/DevZone/guides/ebayfeatures/Basics/Call-StandardCallData.html#StandardOutputData

<?xml version="1.0" encoding="utf-8"?>
<GeteBayOfficialTimeRequest xmlns="urn:ebay:apis:eBLBaseComponents">
  <RequesterCredentials>
    <eBayAuthToken> Token goes here </eBayAuthToken>
  </RequesterCredentials>
  <Version>383</Version>
</GeteBayOfficialTimeRequest>