Reputation: 5437
i am trying to parse a xml data using the an api url which i have used in the following code. when i run this url directly in the browser it gives me a xml formatted data like below.
<Error>
<Code>AccessDenied</Code>
<Message>Access Denied</Message>
<RequestId>B4DE627B9936548B</RequestId>
<HostId>LyB4mVbYLYF/a26Wn04sSuMlTwQjLozl11O9Ql2YbMwHgenXRUCd7WAn5QPRa6nj</HostId>
</Error>
but i want this xml in an array then i will itrate that array using foreach loop.
curl_setopt($ch, CURLOPT_URL,'http://mybucket.s3.amazonaws.com
/11111.mpg&mpaction=convert%20format=flv');
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Your application name');
$query = curl_exec($ch);
curl_close($ch);
echo $query;
?>
when i print the variable $query it gives me only the text of xml. i want this xml in an array. please help me
Upvotes: 0
Views: 525
Reputation: 198214
To convert the XML into an array, as alexn already suggested, you can make use of simplexml_load_string
and cast it's return value to an array:
$array = (array) simplexml_load_string($query);
print_r($array);
That gives you:
Array
(
[Code] => AccessDenied
[Message] => Access Denied
[RequestId] => B4DE627B9936548B
[HostId] => LyB4mVbYLYF/a26Wn04sSuMlTwQjLozl11O9Ql2YbMwHgenXRUCd7WAn5QPRa6nj
)
Which might be exactly what you're looking for.
Upvotes: 0
Reputation: 59002
CURL is not used for pasing data. You'll have to resort to some library for that. For your case, i recommend using SimpleXML. Example:
$xml = simplexml_load_string($query);
var_dump($xml);
You can then access the various properties using standard array indexes:
echo $xml['Message']; // Yields "Access Denied"
Upvotes: 1