this question has answer here:
- how parse , process html/xml in php? 27 answers
i getting xml reponse server
<?xml version='1.0' encoding='utf-8' standalone='no'?><registrationresponse><statuscode>20</statuscode><status>1234</status></registrationresponse>
i want retrieve values xml if directly use string able values separately.
$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 try directly take response api this
$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 coming this:
array(1) { [0]=> string(147) "201234" } null null
i want result getting in 1st case can input manually every time want use variable instead of using xml string.
if getting xml url using curl can read xml below
public function get_data_from_url() { $url = "xml url"; $ch = curl_init(); // set options (most optional) // set url 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 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 given url, , return output $output = curl_exec($ch); // close curl resource, , free system resources curl_close($ch); // parse xml simplexml if(!empty($output)) { $livescore_data = new simplexmlelement(stripslashes($output)); return $livescore_data; } }
Comments
Post a Comment