作者热门文章
- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我有这个参数来下载一个 XML 文件:
wget --http-user=user --http-password=pass http://www.example.com/file.xml
我必须如何在 php 中使用它来打开这个 xml 文件?
最佳答案
wget
wget
是一个 linux 命令,而不是 PHP 命令,所以要运行它你需要使用 exec
,这是一个用于执行 shell 命令的 PHP 命令。
exec("wget --http-user=[user] --http-password=[pass] http://www.example.com/file.xml");
如果您正在下载一个大文件并希望监控进度,这会很有用,但是在处理您只对内容感兴趣的页面时,可以使用一些简单的函数来完成此操作。
exec
函数默认启用,但在某些情况下可能会被禁用。此配置选项驻留在您的 php.ini
中,以从 disabled_functions
配置字符串中启用、删除 exec
。
备选方案
使用 file_get_contents
我们可以检索指定 URL/URI 的内容。当您只需要将文件读入变量时,这将是替代 curl 的完美功能 - 请遵循 URI syntax在构建您的 URL 时。
// standard url
$content = file_get_contents("http://www.example.com/file.xml");
// or with basic auth
$content = file_get_contents("http://user:pass@www.example.com/file.xml");
如 Sean the Bean
所述 - 您可能还需要更改 allow_url_fopen
在您的 php.ini 中设置为 true
以允许在此方法中使用 URL,但是,这应该默认为 true。
如果你想在本地存储那个文件,有一个函数 file_put_contents
将其写入文件,结合前面的内容,这可以模拟文件下载:
file_put_contents("local_file.xml", $content);
关于php - 如何在 php 中使用 wget?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14745587/
我是一名优秀的程序员,十分优秀!