【问题标题】:How to access data from xml response using PHP cURL?如何使用 PHP cURL 从 xml 响应中访问数据?
【发布时间】:2020-08-22 18:08:17
【问题描述】:

我向网站发出 cURL 请求,返回的响应是 XML 格式的。请求已成功完成,资源已成功创建。我习惯用 json 访问数据,但是当返回的响应是 XML 时,我如何访问数据/值?

PHP

$ch = curl_init($connection_details['api_host_port']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $data);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);

$response = curl_exec($ch);

header('Content-type: application/xml');
print_r($response);

回应

<?xml version='1.0' encoding='UTF-8' standalone='no'?>
<!DOCTYPE OPS_envelope SYSTEM 'ops.dtd'>
<OPS_envelope>
    <header>
        <version>0.9</version>
    </header>
    <body>
        <data_block>
            <dt_assoc>
                <item key="protocol">XCP</item>
                <item key="action">REPLY</item>
                <item key="object">DOMAIN</item>
                <item key="is_success">1</item>
                <item key="response_code">200</item>
                <item key="response_text">
Domain registration successfully completed. WHOIS Privacy successfully enabled. Domain successfully locked.
</item>
                <item key="attributes">
                    <dt_assoc>
                        <item key="admin_email">jsmith@example.com</item>
                        <item key="whois_privacy_state">enabled</item>
                        <item key="registration_text">Domain registration successfully completed. WHOIS Privacy successfully enabled. Domain
successfully locked.</item>
                        <item key="registration_code">200</item>
                        <item key="id">3735281</item>
                        <item key="cancelled_orders">
                            <dt_array>
                                <item key="0">3764860</item>
                                <item key="1">3764861</item>
                            </dt_array>
                        </item>
                    </dt_assoc>
                </item>
            </dt_assoc>
        </data_block>
    </body>
</OPS_envelope>

【问题讨论】:

标签: php xml curl


【解决方案1】:

这对我有用

$ch = curl_init($connection_details['api_host_port']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $data);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
$response = curl_exec($ch);
header('Content-type: application/xml');
//parse xml string into SimpleXML objects
$xml = simplexml_load_string($response);
print_r($xml);

示例响应

SimpleXMLElement Object
(
    [header] => SimpleXMLElement Object
        (
            [version] => 0.9
        )

    [body] => SimpleXMLElement Object
        (
            [data_block] => SimpleXMLElement Object
                (
                    [dt_assoc] => SimpleXMLElement Object
                        (
                            [item] => Array
                                (
                                    [0] => 200
                                    [1] => Domain registration successfully completed.
Domain successfully locked.
Whois Privacy successfully enabled.
                                    [2] => 1
                                    [3] => DOMAIN
                                    [4] => SimpleXMLElement Object
                                        (
                                            [@attributes] => Array
                                                (
                                                    [key] => attributes
                                                )

                                            [dt_assoc] => SimpleXMLElement Object
                                                (
                                                    [item] => Array
                                                        (
                                                            [0] => Domain registration successfully completed.
Domain successfully locked.
Whois Privacy successfully enabled.
                                                            [1] => 2470645
                                                            [2] => 21834436
                                                            [3] => adams@example.com
                                                            [4] => 200
                                                        )

                                                )

                                        )

                                    [5] => XCP
                                    [6] => REPLY
                                )

                        )

                )

        )

)

【讨论】:

    猜你喜欢
    • 2018-02-03
    • 1970-01-01
    • 1970-01-01
    • 2013-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-28
    • 1970-01-01
    相关资源
    最近更新 更多