本文介绍了如何使用PHP解析来自PayPal API的SOAP响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为PHP中的PayPal集成解析SOAP响应,并且已经阅读了以下文章:

I am trying to parse a SOAP response for a PayPal integration in PHP, and I have read through the following posting:

使用具有多个名称空间的SimpleXML解析XML

可以提供深刻的见解,但我仍然无法弄清楚如何正确地做到这一点.这个SOAP事情太复杂了!

which gives great insights, but I am still unable to figure out how to do it correctly. This SOAP thing is damn complicated!

这是我需要解析的SOAP响应:

Here is the SOAP response I need to parse:

<?xml version="1.0" encoding="UTF-8"?>
<soap-env:envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soap-enc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance" xmlns:xsd="http://www.w3.org/1999/XMLSchema" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:cc="urn:ebay:apis:CoreComponentTypes" xmlns:wsu="http://schemas.xmlsoap.org/ws/2002/07/utility" xmlns:saml="urn:oasis:names:tc:SAML:1.0:assertion" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:wsse="http://schemas.xmlsoap.org/ws/2002/12/secext" xmlns:ed="urn:ebay:apis:EnhancedDataTypes" xmlns:ebl="urn:ebay:apis:eBLBaseComponents" xmlns:ns="urn:ebay:api:PayPalAPI">
<soap-env:header>
    <security xmlns="http://schemas.xmlsoap.org/ws/2002/12/secext" xsi:type="wsse:SecurityType">
</security>
<requestercredentials xmlns="urn:ebay:api:PayPalAPI" xsi:type="ebl:CustomSecurityHeaderType">
    <credentials xmlns="urn:ebay:apis:eBLBaseComponents" xsi:type="ebl:UserIdPasswordType">
        <username xsi:type="xs:string"/>
        <password xsi:type="xs:string"/>
        <signature xsi:type="xs:string"/>
        <subject xsi:type="xs:string"/>
    </credentials>
</requestercredentials>
</soap-env:header>
<soap-env:body id="_0">
    <getexpresscheckoutdetailsresponse xmlns="urn:ebay:api:PayPalAPI">
        <timestamp xmlns="urn:ebay:apis:eBLBaseComponents">2012-01-30T16:27:02Z</timestamp>
        <ack xmlns="urn:ebay:apis:eBLBaseComponents">Success</ack>
        <correlationid xmlns="urn:ebay:apis:eBLBaseComponents">24f704da11e31</correlationid>
        <version xmlns="urn:ebay:apis:eBLBaseComponents">85.0</version>
        <build xmlns="urn:ebay:apis:eBLBaseComponents">2515991</build>
        <getexpresscheckoutdetailsresponsedetails xmlns="urn:ebay:apis:eBLBaseComponents" xsi:type="ebl:GetExpressCheckoutDetailsResponseDetailsType">
            <token xsi:type="ebl:ExpressCheckoutTokenType">EC-4AP53152LD0032109</token>
            <payerinfo xsi:type="ebl:PayerInfoType">
                <payer xsi:type="ebl:EmailAddressType">[email protected]</payer>
                <payerid xsi:type="ebl:UserIDType">QWE5QWE4EW</payerid>
                <payerstatus xsi:type="ebl:PayPalUserStatusCodeType">verified</payerstatus>
                <payername xsi:type="ebl:PersonNameType">
                    <salutation xmlns="urn:ebay:apis:eBLBaseComponents"/>
                    <firstname xmlns="urn:ebay:apis:eBLBaseComponents">FirstName</firstname>
                    <middlename xmlns="urn:ebay:apis:eBLBaseComponents"/>
                    <lastname xmlns="urn:ebay:apis:eBLBaseComponents">LastName</lastname>
                    <suffix xmlns="urn:ebay:apis:eBLBaseComponents"/>
                </payername>
                <payercountry xsi:type="ebl:CountryCodeType">US</payercountry>
                <payerbusiness xsi:type="xs:string"/>
                <address xsi:type="ebl:AddressType">
                    <name xsi:type="xs:string"/>
                    <street1 xsi:type="xs:string"/>
                    <street2 xsi:type="xs:string"/>
                    <cityname xsi:type="xs:string"/>
                    <stateorprovince xsi:type="xs:string"/>
                    <countryname/>
                    <postalcode xsi:type="xs:string"/>
                    <addressowner xsi:type="ebl:AddressOwnerCodeType">PayPal</addressowner>
                    <addressstatus xsi:type="ebl:AddressStatusCodeType">None</addressstatus>
                </address>
            </payerinfo>
            <custom xsi:type="xs:string">cutom_value</custom>
            <invoiceid xsi:type="xs:string">PPMP-1393305760</invoiceid>
            <billingagreementacceptedstatus>true</billingagreementacceptedstatus>
            <paymentdetails xsi:type="ebl:PaymentDetailsType">
                <ordertotal xsi:type="cc:BasicAmountType" currencyid="USD">0.02</ordertotal>
                <shippingtotal xsi:type="cc:BasicAmountType" currencyid="USD">0.00</shippingtotal>
                <handlingtotal xsi:type="cc:BasicAmountType" currencyid="USD">0.00</handlingtotal>
                <taxtotal xsi:type="cc:BasicAmountType" currencyid="USD">0.00</taxtotal>
                <orderdescription xsi:type="xs:string">Special Item</orderdescription>
                <custom xsi:type="xs:string">cutom_value</custom>
                <invoiceid xsi:type="xs:string">PPMP-1393305760</invoiceid>
                <shiptoaddress xsi:type="ebl:AddressType">
                    <name xsi:type="xs:string"/>
                    <street1 xsi:type="xs:string"/>
                    <street2 xsi:type="xs:string"/>
                    <cityname xsi:type="xs:string"/>
                    <stateorprovince xsi:type="xs:string"/>
                    <countryname/>
                    <phone xsi:type="xs:string"/>
                    <postalcode xsi:type="xs:string"/>
                    <addressid xsi:type="xs:string"/>
                    <addressowner xsi:type="ebl:AddressOwnerCodeType">PayPal</addressowner>
                    <externaladdressid xsi:type="xs:string"/>
                    <addressstatus xsi:type="ebl:AddressStatusCodeType">None</addressstatus>
                </shiptoaddress>
                <insurancetotal xsi:type="cc:BasicAmountType" currencyid="USD">0.00</insurancetotal>
                <shippingdiscount xsi:type="cc:BasicAmountType" currencyid="USD">0.00</shippingdiscount>
                <insuranceoptionoffered xsi:type="xs:string">false</insuranceoptionoffered>
                <sellerdetails xsi:type="ebl:SellerDetailsType"/>
                <paymentrequestid xsi:type="xs:string"/>
                <orderurl xsi:type="xs:string"/>
                <softdescriptor xsi:type="xs:string"/>
            </paymentdetails>
            <checkoutstatus xsi:type="xs:string">PaymentActionNotInitiated</checkoutstatus>
            <paymentrequestinfo xsi:type="ebl:PaymentRequestInfoType"/>
        </getexpresscheckoutdetailsresponsedetails>
    </getexpresscheckoutdetailsresponse>
</soap-env:body>
</soap-env:envelope>

这是到目前为止我一直在使用的PHP代码,但没有结果:

And here is the PHP code I have been working on so far, but with no results:

$xml = simplexml_load_string($res, NULL, NULL, "http://schemas.xmlsoap.org/soap/envelope/");
$xml->registerXPathNamespace('soap-env', 'http://schemas.xmlsoap.org/soap/envelope/');
$xml->registerXPathNamespace('xsi', 'http://www.w3.org/1999/XMLSchema-instance');
$xml->registerXPathNamespace('wsse', 'http://schemas.xmlsoap.org/ws/2002/12/secext');

foreach($xml->xpath('//getexpresscheckoutdetailsresponse') as $header)
{
    var_export($header->xpath('//timestamp')); // Should output 'something'.
}

$ res包含完整的SOAP响应.我究竟做错了什么?任何帮助,我们都感激不尽!

Where $res includes the complete SOAP response. What am I doing wrong? Any help is really appreciated!

谢谢.

祝一切顺利,

Fab.

推荐答案

您应该考虑使用类似 Zend_Soap_Client -它将为您处理SOAP-XML-而不是您期望的每个响应的自定义xml格式.

You should consider using a library like Zend_Soap_Client - which will handle the SOAP-XML for you - instead of custom xml formatting for each response you expect.

这篇关于如何使用PHP解析来自PayPal API的SOAP响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 07:06