1

I have this proxy done using curl. I t successfully gets the xml to be displayed in the page. I'm trying to save a copy locally with no success, i tried (already in the complete code below):

$fp = fopen('data.xml', 'w'); fwrite($fp, $xml); fclose($fp); 

But I got no error and an empty "data.xml" file...How can this be done? thanks

[EDIT2]: solved, i just added the flag: curl_setopt($session, CURLOPT_RETURNTRANSFER, true); and done! (updated in the code below The code with @spell suggestion.

define ('HOSTNAME', 'http://www.camara.gov.br'); $path = "http://www.camara.gov.br/SitCamaraWS/Deputados.asmx/ObterDeputados?"; $url = HOSTNAME.$path; $session = curl_init($path); curl_setopt($session, CURLOPT_HEADER, false); curl_setopt($session, CURLOPT_HTTPGET, true); curl_setopt($session, CURLOPT_RETURNTRANSFER, true); curl_setopt($session, CURLOPT_USERAGENT, 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.76 Safari/537.36'); header("Content-Type: text/xml"); $xml = curl_exec($session); curl_close($session); file_put_contents ('./data.xml', $xml); ?> 

1 Answer 1

4

You can just use file_put_contents function.

In your case it will be:

// Make the call $xml = curl_exec($session); // done, shutDown. curl_close($session); if (file_put_contents ('./data.xml', $xml) !== false) { echo 'Success!'; } else { echo 'Failed'; } 

And

But I got no error and an empty "data.xml" file

Next time provide an error

Sign up to request clarification or add additional context in comments.

2 Comments

Hi @Spell, thanks for your help. It almost did it, but I still getting an empty file after all... I edited the question with the code that I'm running now, with a link to it too. Also If I leave the echo on, it seems to append the string to end of the xml causing an error, so i stripped it for now. Could you have a look at this again please? Thanks
It was missing curl_setopt($session, CURLOPT_RETURNTRANSFER, true); code edited above. solved. thanks

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.