无法在php中访问xml的节点值

时间:2015-08-11 07:50:40

标签: php xml laravel post curl

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

我的代码看起来像

     $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进行查看时,我只会获得file<- "soil.sil" write.table("[thickness]\nunit=m",file, append = TRUE, row.names=FALSE, quote=FALSE, col.names=FALSE) write.table(paste0("1=",L1), file, append = TRUE, row.names=FALSE, quote=FALSE, col.names=FALSE) write.table(paste0("2=",L2), file, append = TRUE, row.names=FALSE, quote=FALSE, col.names=FALSE) 而没有其他内容。但我想从此xml响应中访问所有Result,OperationId和ResultMessage的值。 但是当我在本地计算机中保存相同的xml响应并尝试获取数据时,我可以获取所有节点值,但它不会从URL发生。我想知道这个问题是什么?

1 个答案:

答案 0 :(得分:0)

我运行此代码并获得一个不错的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变量,而不是响应。