【问题标题】:eBay's finding API with PHP and SOAP itemFilter not workingeBay 使用 PHP 和 SOAP itemFilter 查找 API 不起作用
【发布时间】:2013-11-23 19:51:35
【问题描述】:

我编写了一个简单的 PHP 脚本,用于从 eBay 检索商品。我正在使用 PHP 和 SOAP。我可以通过其他电话获得物品。但是,我需要使用findItemsAdvanced 电话从卖家那里获取商品,但我无法让它工作。

这是我目前所拥有的:

function call_ebay_id_api($itemID){

$location = 'http://svcs.ebay.com/services/search/FindingService/v1?SECURITY-APPNAME=****8&OPERATION-NAME=findItemsAdvanced&itemFilter(0).name=Seller&itemFilter(0).value=****';
$clientebay = new SoapClient(
    "http://developer.ebay.com/webservices/Finding/latest/FindingService.wsdl",
    array(
        "exceptions" => 0,
        "trace" => 1,
        "location"=> $location
        )
);
$requestebay = array("itemFilter" => array("name" => "Seller", "value" => "****"));

$xmlresultsebay = $clientebay->__soapCall('findItemsAdvanced', array($requestebay));
echo "REQUEST HEADERS:\n<pre>" . htmlentities($clientebay->__getLastRequestHeaders()) . "</pre>\n";
echo "REQUEST:\n<pre>" . htmlentities($clientebay->__getLastRequest()) . "</pre>\n";
echo "RESPONSE:\n<pre>" . htmlentities($clientebay->__getLastResponse()) . "</pre>\n";
echo 'd';
return $xmlresultsebay;

来自这里的 XML 请求是:

<?xml version="1.0" encoding="UTF-8"?>
  <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.ebay.com/marketplace/search/v1/services">
     <SOAP-ENV:Body>
         <ns1:findItemsAdvancedRequest>
             <itemFilter>
                 <name>Seller</name>
                 <value>*seller name here*</value>
              </itemFilter>
          </ns1:findItemsAdvancedRequest>
     </SOAP-ENV:Body>
 </SOAP-ENV:Envelope>

这看起来像他们在API's page 上的示例

但是,我得到的回复是错误的。它说我需要指定关键字或类别 ID。当我这样做时,我得到了成功,但 itemFilter 没有被应用。

谁能帮帮我?

【问题讨论】:

    标签: php soap ebay-api


    【解决方案1】:

    好吧,所以我让它工作了......我猜?

    所以我认为问题在于 eBay 的 itemFilter 需要一个 ItemFilter 类型(注意 ItemFilter 中的大写 I,顺便说一句),而不是数组。这导致我创建了与请求调用所需的类型同名的类。所以我创建了名为 ItemFilter、Name、Value 等的对象。但是,它仍然不起作用。

    我几乎放弃了,也许用 cURL 或其他东西重新开始……甚至是 Cajun voodoo,因为其他方法都不起作用,所以嘿,为什么不使用魔法呢?

    当我带着一块鳄鱼皮和一些布丁链环去玛丽·拉沃夫人的墓地戴上她的坟墓后,我真的饿了,开始想布丁。 “嗯……猪肠皮里面的米饭和猪杂……等一下,”我心想,“皮?我应该检查变量是否正确包裹。”所以在我回家后(从当地市场买了更多的布丁),我检查了包装纸,它是正确的。但后来我注意到 itemFilter 的命名空间丢失了。所以我开始搞乱PHP SoapVar ......它奏效了! “终于,在这个被上帝遗弃的 web-dev 深渊中燃起了微弱的火花,”我这样想。

    所以基本上不是:

    $requestebay = array("itemFilter" => array("name" => "Seller", "value" => "****"));
    

    我应该说:

    $itemFilters = array(
        "name" => new SoapVar("Seller", XSD_STRING, NULL, NULL, NULL, 'http://www.ebay.com/marketplace/search/v1/services'),
        "value" => new SoapVar("****", XSD_STRING, NULL, NULL, NULL, 'http://www.ebay.com/marketplace/search/v1/services')
    );
    $requestebay = array(
        "itemFilter" => new SoapVar($itemFilters, SOAP_ENC_OBJECT, NULL, NULL, NULL, 'http://www.ebay.com/marketplace/search/v1/services')
    );
    

    ...和 ​​BAM!这解决了我的问题。我想这里要吸取的教训是,voodoo 比 eBay 的文档要好得多,因为有了 voodoo,我不必深入挖掘来为我的下一个项目寻找东西。

    【讨论】:

      【解决方案2】:

      而不是为每个变量构建一个 Soapvar 创建一个 \SoapClient

      的子类
      namespace whatever;
      class EbaySoapClient extends \SoapClient{
          public function __doRequest($request, $location, $action, $version, $one_way = 0) {
              $request = str_replace(['ns1:', ':ns1'], '', $request);
              $doreq = parent::__doRequest($request, $location, $action, $version, $one_way);
              $this->__last_request = $request;
              return $doreq;
          }
      }
      

      示例调用:

      $wsdl = "http://developer.ebay.com/webservices/Finding/latest/FindingService.wsdl";
      $endpointprod = "http://svcs.ebay.com/services/search/FindingService/v1";
      $endpointbox ="http://svcs.sandbox.ebay.com/services/search/FindingService/v1";
      $sandboxApi = "YourSandboxApiKey";
      $prodApi = "YourProdApiKey";
      
      $headers = stream_context_create(['http' =>
          [   
              'method' => 'POST',
              'header' => implode(PHP_EOL, [
                  'X-EBAY-SOA-SERVICE-NAME: FindingService',
                  'X-EBAY-SOA-OPERATION-NAME: findCompletedItems',
                  'X-EBAY-SOA-SERVICE-VERSION: 1.0.0',
                  'X-EBAY-SOA-GLOBAL-ID: EBAY-US',
                  'X-EBAY-SOA-SECURITY-APPNAME: ' . $sandboxApi,
                  'X-EBAY-SOA-REQUEST-DATA-FORMAT: XML',
                  'X-EBAY-SOA-MESSAGE-PROTOCOL: SOAP12',
                  'Content-Type: text/xml;charset=utf-8',])
              ]
          ]
      );
      $options = [
          'trace' => true,
          'cache' => WSDL_CACHE_NONE,
          'exceptions' => true,
          'soap_version' => SOAP_1_2,
          'location' => $endpointbox,
          'stream_context' => $headers
      ];
      $soap = new \whatever\EbaySoapClient($wsdl, $options);
      $array = [
          'keywords' => 'clock', 
          'itemFilter' => [
              ['name' => 'Condition', 'value' => 3000],
              ['name' => 'FreeShippingOnly', 'value' => 1]
           ]
      
      ];
      $response = $soap->findCompletedItems($array);
      echo $soap->__getLastRequest();
      var_dump($response);
      

      不是最有说服力的,但对我有用,如果你想把命名空间放在那里,你可以放弃命名空间以作说明。它设置默认命名空间。

      【讨论】:

        猜你喜欢
        • 2023-02-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多