Search code examples
phphttphttp-headersfopenfwrite

How to open an http file with fopen()?


I need to open a http external xml file, then replace some text and save the file in the hosting of my site.

Somethinkg like this:

<?php

$xml_external_path = 'http://someplace.com/external_file.xml';
$xml_external = fopen($xml_external_path, "w+");


//here the code to replace text

$xml_local_path = 'http://www.misite.com/local_file.xml';
$xml_local = fopen($xml_sw_path, "w+");

fwrite($xml_local, $xml_external);

fclose($xml_external);
fclose($xml_local);
?>

Problem is that I get this message:

Warning: fopen(http://someplace.com/external_file.xml): failed to open stream: HTTP wrapper does not support writeable connections at...

Warning: fopen(http://someplace.com/local_file.xml): failed to open stream: HTTP wrapper does not support writeable connections at...

Both files are writeable.


Solution

  • You dont want to write to the address. Just replace the +w with r.

    $xml_external_path = 'http://someplace.com/external_file.xml';
    $xml_external = fopen($xml_external_path, "r");
    ....
    

    Have a look at those examples.

    You could also use the file-get-contents function.

    file_get_contents("http://someplace.com/external_file.xml");