How to use wget in php?

后端 未结 10 1971
离开以前
离开以前 2020-12-04 14:24

I have this parameters to download a XML file:

wget --http-user=user --http-password=pass http://www.example.com/file.xml

How I have to use

10条回答
  •  情深已故
    2020-12-04 15:17

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

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

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

提交回复
热议问题