如何通过SoapClient的连接到易趣交易API? [英] How to connect to the ebay trading API through SoapClient?
问题描述
我试图连接到易趣交易API并使用PHP的SoapClient类外一个基本的要求,但我有麻烦了。我已经做了搜索,并用实例摆弄几个小时,但我不能得到任何工作。所以我写了下面的准系统code和我想要得到它的工作:
I'm trying to connect to the ebay trading API and make a basic request using PHP's SoapClient class, but I'm having trouble. I've done hours of searching for and fiddling with examples, but I cannot get anything to work. So I wrote the following barebones code and I'm trying to get it working:
$token = [token here];
$client = new SOAPClient('http://developer.ebay.com/webservices/latest/eBaySvc.wsdl', array('trace' => 1, 'features' => SOAP_SINGLE_ELEMENT_ARRAYS));
$header = new SoapHeader('urn:ebay:apis:eBLBaseComponents', 'RequesterCredentials', new SoapVar(array('ebayAuthToken' => $token), SOAP_ENC_OBJECT), false);
$client->__setSoapHeaders(array($header));
$method = 'GeteBayOfficialTime';
$parameters = array(
);
try {
$responseObj = $client->__soapCall($method, array($parameters));
}
catch (Exception $e)
{
echo 'Exception caught. Here are the xml request & response:<br><br>';
echo '$client->__getLastRequest():<br><pre><xmp>' . $client->__getLastRequest() . '</xmp></pre>';
echo '$client->__getLastResponse():<br><pre><xmp>' . $client->__getLastResponse() . '</xmp></pre><br>';
echo '<p>Exception trying to call ' . $method . '</p>';
echo '$e->getMessage()';
echo '<pre>' . $e->getMessage() . '</pre>';
}
,它的输出是:
Exception caught. Here are the xml request & response:
$client->__getLastRequest():
<?xml version="1.0" encoding="UTF-8"?> <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:ebay:apis:eBLBaseComponents" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><SOAP-ENV:Header><xsd:RequesterCredentials><ebayAuthToken>[token was here]</ebayAuthToken></xsd:RequesterCredentials></SOAP-ENV:Header><SOAP-ENV:Body><ns1:GeteBayOfficialTimeRequest/></SOAP-ENV:Body></SOAP-ENV:Envelope>
$client->__getLastResponse():
<?xml version="1.0" encoding="UTF-8"?> <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <soapenv:Body> <soapenv:Fault> <faultcode>soapenv:Server.userException</faultcode> <faultstring>com.ebay.app.pres.service.hosting.WebServiceDisabledException: The web service eBayAPI is not properly configured or not found and is disabled.</faultstring> <detail/> </soapenv:Fault> </soapenv:Body> </soapenv:Envelope>
Exception trying to call GeteBayOfficialTime
$e->getMessage()
com.ebay.app.pres.service.hosting.WebServiceDisabledException: The web service eBayAPI is not properly configured or not found and is disabled.
谁能帮我得到这个工作?问题的部分原因可能是因为我不知道我应该在SOAPHEADER功能(命名空间)的第一个参数去了。
Can anyone help me get this working? Part of the problem might be that I have no clue what should go in the first parameter of the SoapHeader function ("namespace").
推荐答案
在黑客入侵别人的例子和我自己尝试新东西的时间,我终于能得到这个工作。我在这里张贴的解决方案的情况下,它可以帮助别人:
After hours of hacking other people's examples and trying new stuff on my own, I finally was able to get this working. I'm posting the solution here in case it helps someone else:
$token = '';
$appId = '';
$wsdl_url = 'ebaysvc.wsdl.xml'; // downloaded from http://developer.ebay.com/webservices/latest/eBaySvc.wsdl
$apiCall = 'GetUser';
$client = new SOAPClient($wsdl_url, array('trace' => 1, 'exceptions' => true, 'location' => 'https://api.sandbox.ebay.com/wsapi?callname=' . $apiCall . '&appid=' . $appId . '&siteid=0&version=821&routing=new'));
$requesterCredentials = new stdClass();
$requesterCredentials->eBayAuthToken = $token;
$header = new SoapHeader('urn:ebay:apis:eBLBaseComponents', 'RequesterCredentials', $requesterCredentials);
// the API call parameters
$params = array(
'Version' => 821,
'DetailLevel' => 'ReturnSummary',
'UserID' => ''
);
$responseObj = $client->__soapCall($apiCall, array($params), null, $header); // make the API call
凡 $令牌
, $的appid
和用户名
用适当的值填充。
的几个注意事项:
- 因为异常是设置为true,则SoapClient的构造函数调用,所有的SOAPCall的应该是一个try / catch块内
- 的
SITEID
参数设置为0
,这表明这是美国eBay的网站 - 的位置URL应该从
api.sandbox.ebay.com
更改为api.ebay.com
来来代替沙箱 生产环境 - 我决定要下载的WSDL文件,而不是远程使用它,因为它是非常大(约5MB),减缓请求显著
- Because exceptions are set to true, the SoapClient constructor call and all soapCall's should be inside of a try/catch block
- The
siteid
parameter is set to0
, which indicates this is for the United States ebay website - The location URL should be changed from
api.sandbox.ebay.com
toapi.ebay.com
to use the production environment instead of the sandbox - I decided to download the WSDL file instead of using it remotely because it's very large (about 5MB) and slows down requests significantly
我不知道为什么这样一个简单的例子是不提供任何地方,但我肯定希望它一直当我试图算出这个!
I don't know why a simple example like this isn't available anywhere, but I sure wish it had been when I was trying to figure this out!
这篇关于如何通过SoapClient的连接到易趣交易API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!