需要 paypal 的简单 php SoapClient 示例

发布于 2024-09-06 11:33:46 字数 2822 浏览 3 评论 0原文

我能得到一个简单的例子,使用 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 会抛出“版本不受支持”错误。

Could I get a simple example of using PHP's SoapClient class to make an empty call to Paypal with nothing but the version number? I have the correct WSDL url and server url, so that's not what I need help with. This is what I have:

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

I hope it's okay I am not showing everything. The body of the request comes out completely wrong, as you can see below:

<?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>

It should look like this:

<?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>

Of course, Paypal throws a "Version is not supported" error.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

2024-09-13 11:33:46

这是我能想到的最干净的解决方案:

$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 看起来也不错,但我是一个喜欢惩罚的人,所以我选择了困难的道路。 :-)

This is the cleanest solution I could come up with:

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

This produces the following XML request document, which, at the time of writing, was being successfully accepted and processed by 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>

In response to some of the other comments on this page:

  • I'm fairly certain the OP has read the API doc, because that's where the example XML came from that he is trying to reproduce using PHP SOAP library.
  • The PayPal PHP API has some shortcomings, the biggest one being that it fails to work with E_STRICT warnings turned on. It also requires PEAR, so if you are not currently using PEAR in your project it means dragging in quite a lot of new code, which means more complexity and potentially more risk, in order to achieve what should be two or three fairly simple XML exchanges for a basic implementation.
  • The NVP API looks pretty good too, but I'm a glutten for punishment, so I chose the hard path. :-)
风尘浪孓 2024-09-13 11:33:46

您是否在此处查看了 PayPal 提供的 API ?以及指向 PHP SOAP API 的直接链接下载。

这里是 PayPal 提供的 NVP API 的链接推荐您使用。

Did you check out the API's provided by PayPal here? And a direct link to the PHP SOAP API download.

And here is a link to the NVP API which PayPal recommends you use.

电影里的梦 2024-09-13 11:33:46

PHP 有一个非常易于使用的soapClass。
可以在这里找到。

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

PHP has a very easy to use soapClass.
Which can be found here.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文