这是我能想到的最干净的解决方案:
$client = new SoapClient( 'https://www.sandbox.paypal.com/wsdl/PayPalSvc.wsdl',
array( 'soap_version' => SOAP_1_1 ));
$cred = array( 'Username' => $username,
'Password' => $password,
'Signature' => $signature );
$Credentials = new stdClass();
$Credentials->Credentials = new SoapVar( $cred, SOAP_ENC_OBJECT, 'Credentials' );
$headers = new SoapVar( $Credentials,
SOAP_ENC_OBJECT,
'CustomSecurityHeaderType',
'urn:ebay:apis:eBLBaseComponents' );
$client->__setSoapHeaders( new SoapHeader( 'urn:ebay:api:PayPalAPI',
'RequesterCredentials',
$headers ));
$args = array( 'Version' => '71.0',
'ReturnAllCurrencies' => '1' );
$GetBalanceRequest = new stdClass();
$GetBalanceRequest->GetBalanceRequest = new SoapVar( $args,
SOAP_ENC_OBJECT,
'GetBalanceRequestType',
'urn:ebay:api:PayPalAPI' );
$params = new SoapVar( $GetBalanceRequest, SOAP_ENC_OBJECT, 'GetBalanceRequest' );
$result = $client->GetBalance( $params );
echo 'Balance is: ', $result->Balance->_, $result->Balance->currencyID;
这将生成以下XML请求文档,在撰写本文时,该文档已被PayPal成功接受和处理:
xmlns:ns1="urn:ebay:apis:eBLBaseComponents" xmlns:ns2="urn:ebay:api:PayPalAPI"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
***
***
***
71.0
1
回应此页面上的其他一些评论:
>我非常确定OP已经阅读了API文档,因为这是示例XML的来源,他试图使用PHP SOAP库进行重现.> PayPal PHP API有一些缺点,最大的缺点是无法启用E_STRICT警告.它还需要PEAR,因此如果您当前没有在项目中使用PEAR,则意味着拖入相当多的新代码,这意味着更多的复杂性和更大的风险,以实现应该是两个或三个相当简单的XML交换用于基本实现.> NVP API看起来也很不错,但我受到了惩罚,因此我选择了艰难的道路. 🙂