从api响应中分离xml属性php [duplicate]

时间:2022-10-23 15:37:59

This question already has an answer here:

这个问题在这里已有答案:

i am getting this xml as a reponse from server

我从服务器获取此xml作为响应

<?xml version='1.0' encoding='utf-8' standalone='no'?><REGISTRATIONRESPONSE><STATUSCODE>20</STATUSCODE><STATUS>1234</STATUS></REGISTRATIONRESPONSE>

i want to retrieve values from this xml if i directly use this string like this i am able to get values separately.

我想从这个xml中检索值,如果我直接使用这个字符串,我可以单独获取值。

  $xmlString ="<?xml version='1.0' encoding='utf-8' standalone='no'?>
<REGISTRATIONRESPONSE>
<STATUSCODE>20</STATUSCODE>
<STATUS>1234</STATUS>
</REGISTRATIONRESPONSE>";
 $xml = simplexml_load_string($xmlString);
 $array = (array) $xml;
 var_dump($array);
 var_dump($array['STATUSCODE']);
 var_dump($array['STATUS']);

Result Is:

 array(2) {
 ["STATUSCODE"]=>
 string(2) "20"
 ["STATUS"]=>
 string(4) "1234"
  }
 string(2) "20"
 string(4) "1234"

but when i try directly take the response from api like this

但是当我尝试直接从这样的api采取响应

   $result =curl_exec($ch);
   print_r($result); 
   echo '<pre>';
   $xml = simplexml_load_string($result);
   $xml = simplexml_load_string($result);
   $array = (array) $xml;
   var_dump($array);
   var_dump($array['STATUSCODE']);
   var_dump($array['STATUS']); 

result which is coming is like this:

即将到来的结果是这样的:

   array(1) {
            [0]=>  string(147) "201234"
            }
            NULL
            NULL

I want to get result as i am getting in the 1st case but i can input manually like that every time i want to use variable instead of using xml as a string.

我希望得到结果,因为我在第一种情况下,但我可以像每次我想使用变量而不是使用xml作为字符串手动输入。

1 个解决方案

#1


0  

if you are getting xml from url using curl you can read xml like below

如果你使用curl从url获取xml,你可以读取xml,如下所示

 public function get_data_from_url()
    {

        $Url = "Xml Url";

        $ch = curl_init();

        // Now set some options (most are optional)

        // Set URL to download
        curl_setopt($ch, CURLOPT_URL, $Url);

        // Include header in result? (0 = yes, 1 = no)
        curl_setopt($ch, CURLOPT_HEADER, 0);

        // Should cURL return or print out the data? (true = return, false = print)
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        // Timeout in seconds
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);

        // Set Data Decompression
        curl_setopt($ch, CURLOPT_ENCODING, '');

        // Download the given URL, and return output
        $output = curl_exec($ch);

        // Close the cURL resource, and free system resources
        curl_close($ch);

        // Parse XML with SimpleXML
       if(!empty($output))
        {
        $livescore_data = new SimpleXMLElement(stripslashes($output));
        return $livescore_data;

        }

    }

#1


0  

if you are getting xml from url using curl you can read xml like below

如果你使用curl从url获取xml,你可以读取xml,如下所示

 public function get_data_from_url()
    {

        $Url = "Xml Url";

        $ch = curl_init();

        // Now set some options (most are optional)

        // Set URL to download
        curl_setopt($ch, CURLOPT_URL, $Url);

        // Include header in result? (0 = yes, 1 = no)
        curl_setopt($ch, CURLOPT_HEADER, 0);

        // Should cURL return or print out the data? (true = return, false = print)
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        // Timeout in seconds
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);

        // Set Data Decompression
        curl_setopt($ch, CURLOPT_ENCODING, '');

        // Download the given URL, and return output
        $output = curl_exec($ch);

        // Close the cURL resource, and free system resources
        curl_close($ch);

        // Parse XML with SimpleXML
       if(!empty($output))
        {
        $livescore_data = new SimpleXMLElement(stripslashes($output));
        return $livescore_data;

        }

    }