simple php SoapClient example for paypal needed

前端 未结 3 398
逝去的感伤
逝去的感伤 2020-12-15 15:11

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 ur

相关标签:
3条回答
  • 2020-12-15 15:30

    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. :-)
    0 讨论(0)
  • 2020-12-15 15:30

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

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

    0 讨论(0)
  • 2020-12-15 15:48

    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.

    0 讨论(0)
提交回复
热议问题