简单XML问题

发布于 2024-11-01 19:41:17 字数 3775 浏览 2 评论 0原文

也许有人可以帮助我解决有关 SimpleXML 对象的问题。

我正在与authorize.net CIM 管理器集成并发送客户资料。当我发送它时,原始 xml 响应将通过解析器传递并转换为 SimpleXML 对象。

以下是用于提交请求的代码:

$content =
        "<?xml version=\"1.0\" encoding=\"utf-8\"?>".
        "<createCustomerProfileRequest xmlns=\"AnetApi/xml/v1/schema/AnetApiSchema.xsd\">".
        MerchantAuthenticationBlock().
        "<profile>".
        //"<merchantCustomerId>".$userInsertedId."</merchantCustomerId>". // Your own identifier for the customer.
        "<description>'New User Purchase'</description>".
        "<email>" . $_GET["email"] . "</email>".
        "<paymentProfiles>".
        "<billTo>".
         "<firstName>".$_GET["firstname"]."</firstName>".
         "<lastName>".$_GET["lastname"]."</lastName>".
         "<address>".$_GET["street"]."</address>".
         "<city>".$_GET["city"]."</city>".
         "<state>".$_GET["state"]."</state>".
         "<zip>".$_GET["zip"]."</zip>".
         "<country>".$_GET["country"]."</country>".
         "<phoneNumber>".$_GET["phone"]."</phoneNumber>".
        "</billTo>".
        "<payment>".
         "<creditCard>".
          "<cardNumber>".$_GET["number"]."</cardNumber>".
          "<expirationDate>".$_GET["year"]."-".$_GET["month"]."</expirationDate>". // required format for API is YYYY-MM
          "<cardCode>".$_GET["code"]."</cardCode>".
         "</creditCard>".
         "</payment>".
        "</paymentProfiles>".
        "</profile>".
        "<validationMode>testMode</validationMode>". 
        "</createCustomerProfileRequest>";

        $CCresponse = send_xml_request($content);

        //echo($CCresponse);

        $parsedresponse = parse_api_response($CCresponse);

   function parse_api_response($content)
        {
            $parsedresponse = simplexml_load_string($content, "SimpleXMLElement", LIBXML_NOWARNING);
            if ("Ok" != $parsedresponse->messages->resultCode) 
            {
                echo "The operation failed with the following errors:<br>";
                foreach ($parsedresponse->messages->message as $msg) 
                {
                    echo "[" . htmlspecialchars($msg->code) . "] " . htmlspecialchars($msg->text) . "<br>";
                }
                    echo "<br>";
            }
            return $parsedresponse;
        }

当我执行以下操作时:

$parsedresponse = parse_api_response($CCresponse);
print_r($parsedresponse);

我得到以下输出:

SimpleXMLElement Object
(
[messages] => SimpleXMLElement Object
    (
        [resultCode] => Ok
        [message] => SimpleXMLElement Object
            (
                [code] => I00001
                [text] => Successful.
            )
    )
[customerProfileId] => 15642446
[customerPaymentProfileIdList] => SimpleXMLElement Object
    (
        [numericString] => 13865552
    )
[customerShippingAddressIdList] => SimpleXMLElement Object
    (
    )
[validationDirectResponseList] => SimpleXMLElement Object
    (
        [string] => 1|1|1|(TESTMODE) This transaction has been approved.|000000|P|0|none|Test transaction for ValidateCustomerPaymentProfile.|1.00|CC|auth_only||*****|******||****|*******|******|******|USA|1234567890||[email protected]|none|none|none|none|none|none|none|none|0.00|0.00|0.00|FALSE|none|207BCBBF78E85CF174C87AE286B472D2

Maybe someone can help me with a problem I am having regarding SimpleXML Objects.

I am integrating with the authorize.net CIM manager and sending in a customer profile. When I sent it in, the raw xml response is passed through a parser and turned into a SimpleXML object.

Here is the code that is used to submit the request:

$content =
        "<?xml version=\"1.0\" encoding=\"utf-8\"?>".
        "<createCustomerProfileRequest xmlns=\"AnetApi/xml/v1/schema/AnetApiSchema.xsd\">".
        MerchantAuthenticationBlock().
        "<profile>".
        //"<merchantCustomerId>".$userInsertedId."</merchantCustomerId>". // Your own identifier for the customer.
        "<description>'New User Purchase'</description>".
        "<email>" . $_GET["email"] . "</email>".
        "<paymentProfiles>".
        "<billTo>".
         "<firstName>".$_GET["firstname"]."</firstName>".
         "<lastName>".$_GET["lastname"]."</lastName>".
         "<address>".$_GET["street"]."</address>".
         "<city>".$_GET["city"]."</city>".
         "<state>".$_GET["state"]."</state>".
         "<zip>".$_GET["zip"]."</zip>".
         "<country>".$_GET["country"]."</country>".
         "<phoneNumber>".$_GET["phone"]."</phoneNumber>".
        "</billTo>".
        "<payment>".
         "<creditCard>".
          "<cardNumber>".$_GET["number"]."</cardNumber>".
          "<expirationDate>".$_GET["year"]."-".$_GET["month"]."</expirationDate>". // required format for API is YYYY-MM
          "<cardCode>".$_GET["code"]."</cardCode>".
         "</creditCard>".
         "</payment>".
        "</paymentProfiles>".
        "</profile>".
        "<validationMode>testMode</validationMode>". 
        "</createCustomerProfileRequest>";

        $CCresponse = send_xml_request($content);

        //echo($CCresponse);

        $parsedresponse = parse_api_response($CCresponse);

   function parse_api_response($content)
        {
            $parsedresponse = simplexml_load_string($content, "SimpleXMLElement", LIBXML_NOWARNING);
            if ("Ok" != $parsedresponse->messages->resultCode) 
            {
                echo "The operation failed with the following errors:<br>";
                foreach ($parsedresponse->messages->message as $msg) 
                {
                    echo "[" . htmlspecialchars($msg->code) . "] " . htmlspecialchars($msg->text) . "<br>";
                }
                    echo "<br>";
            }
            return $parsedresponse;
        }

When I do the following:

$parsedresponse = parse_api_response($CCresponse);
print_r($parsedresponse);

I get the following output:

SimpleXMLElement Object
(
[messages] => SimpleXMLElement Object
(
[resultCode] => Ok
[message] => SimpleXMLElement Object
(
[code] => I00001
[text] => Successful.
)
)
[customerProfileId] => 15642446
[customerPaymentProfileIdList] => SimpleXMLElement Object
(
[numericString] => 13865552
)
[customerShippingAddressIdList] => SimpleXMLElement Object
(
)
[validationDirectResponseList] => SimpleXMLElement Object
(
[string] => 1|1|1|(TESTMODE) This transaction has been approved.|000000|P|0|none|Test transaction for ValidateCustomerPaymentProfile.|1.00|CC|auth_only||*****|******||****|*******|******|******|USA|1234567890||[email protected]|none|none|none|none|none|none|none|none|0.00|0.00|0.00|FALSE|none|207BCBBF78E85CF174C87AE286B472D2

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

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

发布评论

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

评论(3

赏烟花じ飞满天 2024-11-08 19:41:17

|||*******|*******

|||*******|*******

著墨染雨君画夕 2024-11-08 19:41:17

||||||
)
)
)
)

所以从这里看来一切都正常。但是,当我尝试深入研究 SimpleXML 对象并执行以下操作时:

echo $code = (string) $parsedresponse->messages->resultCode;

我得到“OkOk”的输出,看起来它运行了两次。这让我快疯了,我不明白这里到底发生了什么。有人可以在这里为我指明正确的方向,以便我可以正常工作吗?

谢谢!

||||||
)
)
)
)

So from this is looks like all is working. But when I try and drill into the SimpleXML object, and do this:

echo $code = (string) $parsedresponse->messages->resultCode;

I am getting an output of "OkOk", it seems like it is running over it twice. This has been driving me absolutely crazy and I can't figure out what the heck is going on here. Can someone please point me in the right direction here so I can get this working?

Thanks!

浅忆流年 2024-11-08 19:41:17

也许您可以尝试使用 xpath 方法来导航 SimpleXMLElement,如下所示:

$code = (string) $parsedresponse->xpath('messages/resultCode');
echo $code;

Maybe you could try and use the xpath method to navigate the SimpleXMLElement like this:

$code = (string) $parsedresponse->xpath('messages/resultCode');
echo $code;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文