【问题标题】:cannot access node values of xml in php无法在 php 中访问 xml 的节点值
【发布时间】:2026-02-06 02:45:01
【问题描述】:

我正在尝试使用 php 访问 xml 的节点值。但我无法访问其中任何一个。但是,该网址正在返回响应。我正在使用 cURL 发送 post 请求。

我的代码看起来像

     $url = (some url);
  $postDatas = 'CompanyCode=' . urlencode($companyCode) . '&ServiceCode=' . urlencode($serviceCode) . '&Account=' .urlencode($account) . '&Special1=' .urlencode($special1) . '&Special2=' .urlencode($special2) . '&ExternalDate=' .urlencode($externalDate) . '&ExternalId=' .urlencode($externalId) . '&SalesPointType=' .urlencode($salesPointType) . '&UserName=' .urlencode($userName) . '&UserPassword=' .urlencode($userPassword);
                        $ch = curl_init( $url );
                        curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
                        curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
                        curl_setopt( $ch, CURLOPT_POST, 1);
                        curl_setopt( $ch, CURLOPT_POSTFIELDS, $postDatas);
                        curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
                        curl_setopt( $ch, CURLOPT_HEADER, 0);
                        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);
                        $response = curl_exec( $ch );
                        curl_close ($ch);
                        print_r($response); 

在执行 print_r($response) 时,我得到了这样的 xml 响应。

  <PPResponse Type="Check" Key="182f55a7-ed3f-e511-940e-005056b219f6" Result="000" OperationId="61289766243683">
      <ResultMessage>Operation was successfully completed.</ResultMessage>
      <ProviderInfo Code="962" Name="BSNL" Country="356" Currency="356">
        <PackagesList>
          <Package ServiceId="1" Name="Package 1.1" Value="110" Description="1.1 USD" />
          <Package ServiceId="1" Name="Package 2.2" Value="220" Description="2.2USD" />
          <Package ServiceId="1" Name="Package 4.4" Value="440" Description="4.4USD" />
        </PackagesList>
      </ProviderInfo>
    </PPResponse>

但是当我使用$xml = simplexml_load_string($response) 并使用print_r($xml) 查看它时,我只得到Operation is completed successfully 而没有别的。但我想从此 xml 响应中访问所有 Result、OperationId 和 ResultMessage 的值。 但是当我在本地计算机中保存相同的 xml 响应并尝试获取数据时,我可以获取所有节点值,但它不会从 url 发生。我想知道这是什么问题?

【问题讨论】:

    标签: php xml laravel post curl


    【解决方案1】:

    我运行这段代码并得到一个不错的 XML ObjectSimpleXMLElement 对象:

    $string = '<PPResponse Type="Check" Key="182f55a7-ed3f-e511-940e-005056b219f6" Result="000" OperationId="61289766243683">
          <ResultMessage>Operation was successfully completed.</ResultMessage>
          <ProviderInfo Code="962" Name="BSNL" Country="356" Currency="356">
            <PackagesList>
              <Package ServiceId="1" Name="Package 1.1" Value="110" Description="1.1 USD" />
              <Package ServiceId="1" Name="Package 2.2" Value="220" Description="2.2USD" />
              <Package ServiceId="1" Name="Package 4.4" Value="440" Description="4.4USD" />
            </PackagesList>
          </ProviderInfo>
        </PPResponse>';
    $text = simplexml_load_string($string);
    echo "<pre>".print_r($text, true)."</pre>";
    

    结果:

    SimpleXMLElement Object
    (
        [@attributes] => Array
            (
                [Type] => Check
                [Key] => 182f55a7-ed3f-e511-940e-005056b219f6
                [Result] => 000
                [OperationId] => 61289766243683
            )
    
        [ResultMessage] => Operation was successfully completed.
        [ProviderInfo] => SimpleXMLElement Object
            (
                [@attributes] => Array
                    (
                        [Code] => 962
                        [Name] => BSNL
                        [Country] => 356
                        [Currency] => 356
                    )
    
                [PackagesList] => SimpleXMLElement Object
                    (
                        [Package] => Array
                            (
                                [0] => SimpleXMLElement Object
                                    (
                                        [@attributes] => Array
                                            (
                                                [ServiceId] => 1
                                                [Name] => Package 1.1
                                                [Value] => 110
                                                [Description] => 1.1 USD
                                            )
    
                                    )
    
                                [1] => SimpleXMLElement Object
                                    (
                                        [@attributes] => Array
                                            (
                                                [ServiceId] => 1
                                                [Name] => Package 2.2
                                                [Value] => 220
                                                [Description] => 2.2USD
                                            )
    
                                    )
    
                                [2] => SimpleXMLElement Object
                                    (
                                        [@attributes] => Array
                                            (
                                                [ServiceId] => 1
                                                [Name] => Package 4.4
                                                [Value] => 440
                                                [Description] => 4.4USD
                                            )
    
                                    )
    
                            )
    
                    )
    
            )
    
    )
    

    确保打印解码后的 xml 变量,而不是响应。

    【讨论】: