php - 当我使用 cURL 发送 xml 时,Ebay api、VerifyAddItem 返回错误

标签 php xml curl

我在调试收到的此错误时遇到问题。
我在 ebay 开发网站上测试了 xml,看看它是否有效,并且没有收到任何错误
但是,当我尝试发送 xml 文件时,我收到错误。

PHP 代码:

function EbayVerifyAddItem($search)
{
    $url= utf8_encode("https://api.ebay.com/ws/api.dll");  //end point
    $xmlrequest = temp/xmlfile.xml; // link to xml file
    $xml = simplexml_load_file($xmlrequest);
    echo"<h3>Ebay Get Category</h3>";
    echo "URL: ".$url."<br>";

    $headers =  array(
    'X-EBAY-API-COMPATIBILITY-LEVEL: 901',
    'X-EBAY-API-DEV-NAME: productionDeveloperKey',
    'X-EBAY-API-SITEID: 3',
    'X-EBAY-API-CALL-NAME: VerifyAddItem',
    'Content-Type: text/xml');

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);    // set headers using $headers array
    curl_setopt($ch, CURLOPT_POST, true);              // POST request type
    curl_setopt($ch, CURLOPT_POSTFIELDS, $xml); // set the body of the POST
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);    // return values as a string, not to std out
    $content=curl_exec($ch);
    curl_close($ch); 

    echo "<br><br>";
    echo "Path to xml file: ".$xmlrequest."<br>"; 
    echo printf("content: %s",$content); 
    echo "<br><br>";

    return true;
}

xml文件.xml:

<?xml version="1.0" encoding="utf-8"?>
<VerifyAddItemRequest xmlns="urn:ebay:apis:eBLBaseComponents">
  <ErrorLanguage>en_US</ErrorLanguage>
  <WarningLevel>High</WarningLevel>
  <Item>
    <Title>Title</Title>
    <Description>Description</Description>
    <PrimaryCategory>
      <CategoryID>377</CategoryID>
    </PrimaryCategory>
    <StartPrice>1.00</StartPrice>
    <CategoryMappingAllowed>true</CategoryMappingAllowed>
    <ConditionID>1000</ConditionID>
    <Country>GB</Country>
    <Currency>GBP</Currency>
    <DispatchTimeMax>3</DispatchTimeMax>
    <ListingDuration>Days_7</ListingDuration>
    <ListingType>Chinese</ListingType>
    <PaymentMethods>PayPal</PaymentMethods>
    <PayPalEmailAddress><a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b3d6ded2dadff3d6ded2dadf9dd0dcde" rel="noreferrer noopener nofollow">[email protected]</a></PayPalEmailAddress>
    <PictureDetails>
      <PictureURL>http://i1.sandbox.ebayimg.com/03/i/00/6b/63/03_1.JPG?set_id=8800005007 </PictureURL>
    </PictureDetails>
    <PostalCode>postcode</PostalCode>
    <Quantity>1</Quantity>
    <ShippingDetails>
      <ShippingType>Flat</ShippingType>
      <ShippingServiceOptions>
        <ShippingServicePriority>1</ShippingServicePriority>
        <ShippingService>UK_RoyalMailSecondClassRecorded</ShippingService>
        <ShippingServiceCost>2.50</ShippingServiceCost>
      </ShippingServiceOptions>
    </ShippingDetails>
    <Site>UK</Site>
  </Item>
  <RequesterCredentials>
    <eBayAuthToken>Token String Here</eBayAuthToken>
  </RequesterCredentials>
</VerifyAddItemRequest>

我收到的错误:

Ebay Get Category
URL: https://api.ebay.com/ws/api.dll



Path to xml file: functions/tmp/Test.xml
content: 2015-08-15 21:00:07 100121SeriousError00RequestError 51SeriousError00RequestError 100111SeriousError00RequestError 100111SeriousError00RequestError 1115

任何人都可以阐明我哪里出错了。如果我可以验证该项目将成功列出,我可以继续填写其他详细信息并列出该项目。

最佳答案

I am having trouble debugging this error i am recieving.

您看到的“错误”是浏览器过滤时没有标签的 XML。您必须查看源代码(在浏览器中查看源代码功能)或对其进行正确编码,甚至将其解析为 XML 并从中获取错误信息(当您使用 API 时最有意义)。

有关错误的最有可能的重要信息就这样丢失了。最突出的似乎仍然是:

SeriousError00RequestError51

the vendors documentation about the error codes中无法获取具体的错误信息(它指定了几乎所有错误代码),这表明您到目前为止已经阻止了自己清楚地了解错误。

但是,您的问题很可能是您不了解需要将 XML 作为字符串传递到服务端点。

当您使用 XML 解析器验证文件包含有效的 XML 时(这很好!),您错过了将 XML 作为字符串传递给 Web 服务的情况:

curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);

因为 $xml 不是字符串,而是 SimpleXMLElement (这将有效地触发 $xml->__toString() 返回文本内容而不是整个 XML)正确的形式是使用 SimpleXMLElement::asXML() method关于它:

curl_setopt($ch, CURLOPT_POSTFIELDS, $xml->asXML());

就这样了。

关于php - 当我使用 cURL 发送 xml 时,Ebay api、VerifyAddItem 返回错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32028731/

相关文章:

java - 如何在现有的 xml 文件中插入新节点

php - Elasticsearch批量数据插入-JsonParseException [意外字符-使用PHP

PHP curl 头

php - 如何仅使用PHP检查某个坐标是否落入另一个坐标半径

php - 如何根据用户输入输出 HTML 页面

c# - 如何在不知道级别的情况下获取具有相同名称的所有 XML 节点?

c++ - 使用 libcurl 和 SCons C++ 的问题

PHP OAuth 1.0 库,用于处理 api key / secret 对和端点(请求、授权和访问)

php - 更新 MySQL 中 DATETIME 已过的所有行

xml - 新手 : XSLT Transformation to validate rules in XML document