2016-10-01 77 views
2

我想給eBay api打個電話,並通過關鍵字查找產品。隨着eBay documentation和其他在線資源的幫助下,我想出了這個代碼:如何使用PHP對Ebay API進行肥皂調用?

$client = new \SoapClient('http://developer.ebay.com/webservices/finding/latest/FindingService.wsdl'); 
$soap_headers = array(
    new \SoapHeader('X-EBAY-SOA-OPERATION-NAME', 'findItemsByKeywords'), 
    new \SoapHeader('X-EBAY-SOA-SERVICE-VERSION', '1.3.0'), 
    new \SoapHeader('X-EBAY-SOA-REQUEST-DATA-FORMAT', 'XML'), 
    new \SoapHeader('X-EBAY-SOA-GLOBAL-ID', 'EBAY-US'), 
    new \SoapHeader('X-EBAY-SOA-SECURITY-APPNAME', '<there would be the key>'), 
); 
$client->__setSoapHeaders($soap_headers); 

// Call wsdl function 
$result = $client->__soapCall("findItemsByKeywords", array("keywords" => "Potter")); 

但是,此代碼導致錯誤: 「服務操作是未知的, 500內部服務器錯誤 - 的SOAPFault」

我試圖改變的第一行到這個(不知道爲什麼它應該有所作爲,但我看到它的地方):

$client = new \SoapClient(NULL, array(
"location" => 'http://svcs.sandbox.ebay.com/services/search/FindingService/v1', 
'uri' => 'http://svcs.sandbox.ebay.com/services/search/FindingService/v1') 
); 

一第二,現在這個結果在此錯誤:缺少SOA業務名頭, 500內部服務器錯誤 - 的SOAPFault

有誰知道是什麼原因導致出現這些錯誤,以及如何解決這些問題?

謝謝,邁克!

回答

0

事情是,服務期望HTTP標題(或查詢字符串參數,顯然,通過閱讀他們的指導一點)。

With __setSoapHeaders您好,設置SOAP標題。

試試這個,例如

$wsdl = 'http://developer.ebay.com/webservices/finding/latest/FindingService.wsdl'; 

$appID = 'YOUR KEY/ID'; 

$headers = implode(PHP_EOL, [ 
    "X-EBAY-SOA-OPERATION-NAME: findItemsByKeywords", 
    "X-EBAY-SOA-SECURITY-APPNAME: $appID" 
]); 

$options = [ 
    'trace' => true, 
    'cache' => WSDL_CACHE_NONE, 
    'exceptions' => true, 
    'stream_context' => stream_context_create([ 
    'http' => [ 
     'header' => $headers 
    ] 
    ]), 
]; 

$client = new SoapClient($wsdl, $options); 

$response = $client->findItemsByKeywords([ 
    'keywords' => 'Potter' 
]); 
+0

嗨Alejadro。我試過了,代碼在身份驗證中失敗了,所以我發現有必要在頭文件之間添加逗號。無論如何,現在有和以前一樣的錯誤:「服務操作findItemsByKeywords,是未知的」,它在_soapCall(..)方法上失敗。 – Mike

+0

@Mike是的,由於逗號,該服務正在嘗試查找'findItemsByKeywords',現在。刪除逗號。標題應該用換行符分隔。查看更新的答案 – alepeino

1

下面的代碼工作。

define('APP_ID', '*** Your App ID ***'); 
$wsdl = 'http://developer.ebay.com/webservices/finding/latest/FindingService.wsdl'; 
// For sandbox 
$endpoint_uri = 'http://svcs.sandbox.ebay.com/services/search/FindingService/v1'; 
// For production 
//$endpoint_uri = 'http://svcs.ebay.com/services/search/FindingService/v1'; 

// We'll use this namespace explicitly for the 'keywords' tag, 
// because the SoapClient doesn't apply it automatically. 
$ns = 'http://www.ebay.com/marketplace/search/v1/services'; 
// The SOAP function 
$operation = 'findItemsByKeywords'; 

$http_headers = implode(PHP_EOL, [ 
    "X-EBAY-SOA-OPERATION-NAME: $operation", 
    "X-EBAY-SOA-SECURITY-APPNAME: " . APP_ID, 
]); 

$options = [ 
    'trace' => true, 
    'cache' => WSDL_CACHE_NONE, 
    'exceptions' => true, 
    'location' => $endpoint_uri, 
    //'uri' => 'ns1', 
    'stream_context' => stream_context_create([ 
    'http' => [ 
     'method' => 'POST', 
     'header' => $http_headers, 
    ] 
    ]), 
]; 

$client = new \SoapClient($wsdl, $options); 

try { 
    $wrapper = new StdClass; 
    $wrapper->keywords = new SoapVar('harry potter', XSD_STRING, 
    null, null, null, $ns); 

    $result = $client->$operation(new SoapVar($wrapper, SOAP_ENC_OBJECT)); 
    var_dump($result); 
} catch (Exception $e) { 
    echo $e->getMessage(); 
} 

如前所述,其中一個問題是,你通過X-EBAY-SOA-*SOAP頭。該service expects他們作爲HTTP頭:

HTTP Headers or URL Parameters—Each Finding API call requires certain HTTP headers or URL parameters. For example, you must specify your AppID in the X-EBAY-SOA-SECURITY-APPNAME header or SECURITY-APPNAME URL parameter. Similarly, you must always specify the call name in the X-EBAY-SOA-OPERATION-NAME header or OPERATION-NAME URL parameter. Other headers are optional or conditionally required.

第二個問題是,沒有指定SoapClientlocation選項。它應該包含一個到service endpoints之一的URI。否則,該API將返回Service operation is unknown錯誤。

最後,SoapClient不把keywords到易趣的命名空間:

<?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:findItemsByKeywordsRequest> 
     <keywords>harry potter</keywords> 
    </ns1:findItemsByKeywordsRequest> 
    </SOAP-ENV:Body> 
</SOAP-ENV:Envelope> 

其結果是,API將返回一個錯誤:Keywords value required.。所以我明確指定了keywords標記的名稱空間。

還要注意沙箱和生產中使用不同的端點URI。

+0

感謝您提供的woking代碼和解釋!最後,我已經能夠讓它運行! – Mike