2013-11-23 43 views
2

因此我編寫了一個簡單的php腳本,用於從eBay獲取物品。我使用PHP和SOAP。我可以通過其他通話獲得項目。但是,我需要使用findItemsAdvanced調用來獲取賣家的物品,但我無法使其工作。eBay使用PHP和SOAP查找API itemFilter無效

這是我到目前爲止有:

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不適用。

任何人都可以幫助我嗎?

回答

1

Mkay,所以我得到它的工作......我猜?

所以我認爲問題是eBay的itemFilter需要一個ItemFilter類型(注意ItemFilter中的大寫字母btw),而不是數組。這使我創建了與請求調用所需類型相同名稱的類。所以我創建了一個名爲ItemFilter,Name,Value等的對象,但它仍然無法工作。

我幾乎放棄了,並開始可能與cURL或什麼...甚至Cajun巫術,因爲沒有別的工作,所以嘿,爲什麼不魔術?

當我帶着一塊鱷魚皮和一些布丁連接去她的墳墓時,我真的很餓,開始考慮布丁。 「嗯......美味的大米和豬腸內的豬內臟...等等,」我想我自己「包裝?我應該檢查變量是否被正確包裝。」所以在我回家後(還有一些來自當地市場的布丁),我檢查了包裝,這是正確的。但後來我注意到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!這解決了我的問題。我想這裏學到的教訓是,voodoo比eBay的文檔要好很多,因爲使用voodoo我不需要深入挖掘我的下一個項目。

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); 

不是最有說服力的,但對我的作品,你就廢了命名空間如果你想把它放在那裏作說明之用。它設置了默認的命名空間。