需要paypal的简单php SoapClient示例

时间:2010-06-23 21:08:16

标签: php paypal soap-client

我可以得到一个简单的例子,使用PHP的SoapClient类对Paypal进行空调,除了版本号之外什么都没有?我有正确的WSDL URL和服务器URL,所以这不是我需要帮助的。这就是我所拥有的:

public function SOAPcall($function, $args=array()) {
    $args['Version'] = '63.0';
    $args = new SoapVar($args, SOAP_ENC_ARRAY, $function.'_Request');
    $args = array(new SoapVar($args, SOAP_ENC_ARRAY, $function.'_Req', 'urn:ebay:api:PayPalAPI'));
    $results = $this->soapClient->__soapCall($function, $args, array('location' => $this->activeKeys['certificate']), $this->soapOptions);
}

我希望没关系,我没有展示一切。请求正文完全错误,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ns1="urn:ebay:api:PayPalAPI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns2="urn:ebay:apis:eBLBaseComponents">
      <SOAP-ENV:Header>
            <ns1:RequesterCredentials>
                  <ns2:Credentials>
                        <ns2:Username>xxx</ns2:Username>
                        <ns2:Password>xxx</ns2:Password>
                        <ns2:Signature>xxx</ns2:Signature>
                  </ns2:Credentials>
            </ns1:RequesterCredentials>
      </SOAP-ENV:Header>
      <SOAP-ENV:Body>
            <ns1:GetBalanceReq xsi:type="ns1:GetBalance_Req">
                  <xsd:string>63.0</xsd:string>
            </ns1:GetBalanceReq>
      </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

它应该是这样的:

<?xml version=”1.0” encoding=”UTF-8”?>
<SOAP-ENV:Envelope xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xmlns:SOAP-ENC=”http://schemas.xmlsoap.org/soap/encoding/”
xmlns:SOAP-ENV=”http://schemas.xmlsoap.org/soap/envelope/”
xmlns:xsd=”http://www.w3.org/2001/XMLSchema”
SOAP-ENV:encodingStyle=”http://schemas.xmlsoap.org/soap/encoding/”
><SOAP-ENV:Header>
<RequesterCredentials xmlns=”urn:ebay:api:PayPalAPI”>
<Credentials xmlns=”urn:ebay:apis:eBLBaseComponents”>
<Username>api_username</Username>
<Password>api_password</Password>
<Signature/>
<Subject/>
</Credentials>
</RequesterCredentials>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<specific_api_name_Req xmlns=”urn:ebay:api:PayPalAPI”>
<specific_api_name_Request>
<Version xmlns=urn:ebay:apis:eBLBaseComponents”>service_version
</Version>
<required_or_optional_fields xsi:type=”some_type_here”> data
</required_or_optional_fields>
</specific_api_name_Request>
</specific_api_name_Req>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

当然,Paypal会抛出“不支持版本”错误。

3 个答案:

答案 0 :(得分:6)

这是我能提出的最简洁的解决方案:

$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成功接受和处理:

<?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:ns2="urn:ebay:api:PayPalAPI"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <SOAP-ENV:Header>
  <ns2:RequesterCredentials>
   <ns1:Credentials xsi:type="Credentials">
    <Username>***</Username>
    <Password>***</Password>
    <Signature>***</Signature>
   </ns1:Credentials>
  </ns2:RequesterCredentials>
 </SOAP-ENV:Header>
 <SOAP-ENV:Body>
  <ns2:GetBalanceReq xsi:type="GetBalanceRequest">
   <GetBalanceRequest xsi:type="ns2:GetBalanceRequestType">
    <ns1:Version>71.0</ns1:Version>
    <ns2:ReturnAllCurrencies>1</ns2:ReturnAllCurrencies>
   </GetBalanceRequest>
  </ns2:GetBalanceReq>
 </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

回应此页面上的其他一些评论:

  • 我非常确定OP 已经读取了API文档,因为这是示例XML的来源,他试图使用PHP SOAP库进行重现。
  • PayPal PHP API有一些缺点,最大的缺点是无法启用E_STRICT警告。它还需要PEAR,因此如果您当前没有在项目中使用PEAR,则意味着拖入相当多的新代码,这意味着更多的复杂性和更大的风险,以实现应该是两个或三个相当简单的XML交换用于基本实现。
  • NVP API看起来也很不错,但是我受到了惩罚,所以我选择了硬道路。 : - )

答案 1 :(得分:5)

您是否查看了PayPal here提供的API?和PHP SOAP API下载direct link

以下是PayPal建议您使用的NVP API链接。

答案 2 :(得分:-1)

PHP有一个非常好用的soapClass。 哪个可以在这里找到。

http://www.php.net/manual/en/class.soapclient.php