eBay使用PHP和SOAP itemFilter查找API无法正常工作

时间:2013-11-23 19:51:36

标签: php soap ebay

所以我写了一个简单的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。

任何人都可以帮助我吗?

2 个答案:

答案 0 :(得分:1)

Mkay所以我让它工作......我猜?

所以我认为问题是eBay的itemFilter需要一个ItemFilter类型(注意ItemFilter中的大写字母,顺便说一句),而不是数组。这使我创建了与请求调用所需类型相同的类。所以我创建了名为ItemFilter,Name,Value等的对象。但是,它仍然没有用。

我差点放弃并重新开始或许可以使用cURL或其他东西......甚至Cajun voodoo,因为没有其他工作,所以嘿,为什么不魔术呢?

在我带着一条鳄鱼皮和一些boudin链接到她的坟墓上去玛丽拉沃女士的墓地后,我真的很饿,开始考虑布丁。 “嗯......猪肠内包装好吃的米饭和猪内脏...坚持下去,”我心里想着“包裹?我应该检查变量是否包裹正确。”所以在我回到家之后(从当地市场获得了更多的boudin),我检查了包装器,这是正确的。但后来我注意到itemFilter的命名空间丢失了。所以我开始搞乱PHP SoapVar ......它有效! “最后,这个神的暗淡火花抛弃了网络开发的深渊,”所以我想。

所以基本上代替:

$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!这解决了我的问题。我想在这里要学到的教训是,伏都教比eBay的文档好很多,因为对于伏都教而言,我不需要深入挖掘我的下一个项目。

答案 1 :(得分:1)

而不是为每个变量构建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);

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