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

标签 php http http-headers fopen fwrite

我需要打开一个 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"); 

关于php - 如何用 fopen() 打开一个 http 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29098512/

相关文章:

php - 如何更新数据库中的多个字段

php - 为什么 Doctrine 说找不到 PDO 驱动程序?

javascript - 如何使用 javascript 发布文本 '&nsbp '?

android - React-Native 通过 HTTP 发送 multipart/form-data

json - Json RPC v2 over HTTP 的正确内容类型 header 是什么?

php - 如何在 laravel 5.2 的 Controller 中访问 url 段

php - http_build_query 同名参数

Python 'requests' 库 - 定义特定的 DNS?

javascript - 如何使用 express/node.js 配置 CSP-headers?

javascript - 如何修改这个 axios 对象以便我可以在各种 header 之间进行选择?