如何用 fopen() 打开一个 http 文件?

How to open an http file with fopen()?

我需要打开一个 http 外部 xml 文件,然后替换一些文本并将文件保存在我的网站的主机中。

有些想法是这样的:

<?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);
?>

问题是我收到这条消息:

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...

两个文件都是可写的。

您不想写入该地址。只需将 +w 替换为 r.

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

查看 those 个示例。

您也可以使用 file-get-contents 函数。

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