0

I am using below code for converting xml response to php string but I am getting below error

"Warning: simplexml_load_string(): Entity: line 1: parser error : Start tag expected, '<' not found....."
"Warning: simplexml_load_string(): example.com...."
"Warning: simplexml_load_string(): ^ in /home/example/public_html/example.php on line 9"

<?php
echo '<?xml version="1.0" encoding="utf-8" ?>';
$url= "example.com";
$xml = simplexml_load_string($url);
echo $xml->status;
?>
Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
Pratyush Pranjal
  • 544
  • 6
  • 26
  • `$url` is not valid xml (perhaps we should say 'example.php' is not valid XML) - so no surprise at the error –  Jul 08 '15 at 04:24
  • 1
    `simplexml_load_string(file_get_contents($url));` should help... Assuming this is a standard website and `allow_url_fopen` is `on` in your `php.ini` – mccbala Jul 08 '15 at 04:31
  • did you mean `simplexml_load_file($url)` ? ;) – hakre Jul 08 '15 at 20:54

1 Answers1

1

Well, simplexml_load_string expects an XML string to be passed as parameter, which is not the case of your code: the content of $url variable is not a valid XML string, thus, you get those errors.

You may want to load the $url as a file instead. Example:

$xml = simplexml_load_file($url);

Final code:

<?php
echo '<?xml version="1.0" encoding="utf-8" ?>';
$url ="example.com";
$xml = simplexml_load_file($url);
echo $xml->status;
hakre
  • 193,403
  • 52
  • 435
  • 836
Wahib Mkadmi
  • 627
  • 2
  • 9
  • 26