Paul Dessert
Paul Dessert

Reputation: 6389

simplexml_load_file to file creating an empty file

I'm taking an XML file from Amazon and trying to save it to a file.

    $xml = simplexml_load_file($signedUrl);
    $file = "c:/wamp/www/products.xml";
    file_put_contents($file, $xml);

This creates an empty file. Why?

Upvotes: 0

Views: 56

Answers (2)

Yang
Yang

Reputation: 8711

simplexml_load_file() itself returns an object on success, not a string, while file_put_contents() as a second argument expects a string to be written.

So you're trying to save an object instead of raw string.

In this scenario you can simply do something like this:

$xml = file_get_contents($signedUrl);
$file = "c:/wamp/www/products.xml";
file_put_contents($file, $xml);

Since you merely want to save a response into a file.

Upvotes: 1

undone
undone

Reputation: 7888

Second parameter of file_put_contents can be either a string, an array or a stream resource. $xml is an object!

Upvotes: 1

Related Questions