如何在php中使用wget? [英] How to use wget in php?
问题描述
我有以下参数可以下载XML文件:
I have this parameters to download a XML file:
wget --http-user=user --http-password=pass http://www.example.com/file.xml
我如何在php中使用它来打开此xml文件?
How I have to use that in php to open this xml file?
推荐答案
如果目的是仅将内容加载到应用程序中,则甚至不需要使用wget
:
If the aim is to just load the contents inside your application, you don't even need to use wget
:
$xmlData = file_get_contents('http://user:pass@example.com/file.xml');
请注意,如果 allow_url_fopen
,此功能将不起作用在php.ini或Web服务器配置(例如httpd.conf)中被禁用(默认情况下处于启用状态).
Note that this function will not work if allow_url_fopen
is disabled (it's enabled by default) inside either php.ini or the web server configuration (e.g. httpd.conf).
如果您的主机明确禁用了它,或者您正在编写一个库,则建议使用 cURL 或抽象该库的库功能,例如枪口.
If your host explicitly disables it or if you're writing a library, it's advisable to either use cURL or a library that abstracts the functionality, such as Guzzle.
use GuzzleHttp\Client;
$client = new Client([
'base_url' => 'http://example.com',
'defaults' => [
'auth' => ['user', 'pass'],
]]);
$xmlData = $client->get('/file.xml');
这篇关于如何在php中使用wget?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!