0

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

2 Answers 2

1

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"
Sign up to request clarification or add additional context in comments.

1 Comment

@rajzana Just replace your echo $query with my code and you'll see.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.