php 如何在php中使用wget?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14745587/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:53:37  来源:igfitidea点击:

How to use wget in php?

phpwget

提问by Donatas Veikutis

I have this parameters to download a XML file:

我有这个参数来下载一个 XML 文件:

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

How I have to use that in php to open this xml file?

我必须如何在 php 中使用它来打开这个 xml 文件?

采纳答案by Ja?ck

If the aim is to just load the contents inside your application, you don't even need to use wget:

如果目的只是在您的应用程序中加载内容,您甚至不需要使用wget

$xmlData = file_get_contents('http://user:[email protected]/file.xml');

Note that this function will not work if allow_url_fopenis disabled (it's enabled by default) inside either php.ini or the web server configuration (e.g. httpd.conf).

请注意,如果allow_url_fopen在 php.ini 或 Web 服务器配置(例如 httpd.conf)中禁用(默认情况下启用)此功能将不起作用。

If your host explicitly disables it or if you're writing a library, it's advisable to either use cURLor a library that abstracts the functionality, such as Guzzle.

如果您的主机明确禁用它或者您正在编写一个库,则建议使用cURL或抽象功能的库,例如Guzzle

use GuzzleHttp\Client;

$client = new Client([
  'base_url' => 'http://example.com',
  'defaults' => [
    'auth'    => ['user', 'pass'],
]]);

$xmlData = $client->get('/file.xml');

回答by Matt Clark

wget

获取

wgetis a linux command, not a PHP command, so to run this you woud need to use exec, which is a PHP command for executing shell commands.

wget是 linux 命令,而不是 PHP 命令,因此要运行它,您需要使用exec,这是一个用于执行 shell 命令的 PHP 命令。

exec("wget --http-user=[user] --http-password=[pass] http://www.example.com/file.xml");

This can be useful if you are downloading a large file - and would like to monitor the progress, however when working with pages in which you are just interested in the content, there are simple functions for doing just that.

如果您正在下载一个大文件,这会很有用 - 并且想要监控进度,但是在处理您只对内容感兴趣的页面时,有一些简单的功能可以做到这一点。

The execfunction is enabled by default, but may be disabled in some situations. The configuration options for this reside in your php.ini, to enable, remove execfrom the disabled_functionsconfig string.

exec功能默认启用,但在某些情况下可能会被禁用。此配置选项位于您的php.ini, 启用,execdisabled_functions配置字符串中删除。

alternative

选择

Using file_get_contentswe can retrieve the contents of the specified URL/URI. When you just need to read the file into a variable, this would be the perfect function to use as a replacement for curl - follow the URI syntaxwhen building your URL.

使用file_get_contents我们可以检索指定 URL/URI 的内容。当您只需要将文件读入变量时,这将是用作 curl 替代品的完美函数 -在构建 URL 时遵循URI 语法

// standard url
$content = file_get_contents("http://www.example.com/file.xml");

// or with basic auth
$content = file_get_contents("http://user:[email protected]/file.xml");

As noted by Sean the Bean- you may also need to changeallow_url_fopento truein your php.ini to allow the use of a URL in this method, however, this should be true by default.

正如所指出的Sean the Bean- 您可能还需要在 php.ini 中更改允许在此方法中使用 URL,但是,默认情况下这应该是 true。allow_url_fopentrue

If you want to then store that file locally, there is a function file_put_contentsto write that into a file, combined with the previous, this could emulate a file download:

如果您想将该文件存储在本地,则有一个file_put_contents将其写入文件的功能,结合之前的功能,这可以模拟文件下载:

file_put_contents("local_file.xml", $content);

回答by Adam

You can use curlin order to both fetch the data, and be identified (for both "basic" and "digest" auth), without requiring extended permissions (like exec or allow_url_fopen).

您可以使用curl以获取数据并被识别(对于“基本”和“摘要”身份验证),而无需扩展权限(如 exec 或 allow_url_fopen)。

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/file.xml");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, "user:pass");
$result = curl_exec($ch);
curl_close($ch);

Your result will then be stored in the $resultvariable.

然后您的结果将存储在$result变量中。

回答by Navneet Garg

lots of methods available in php to read a file like exec, file_get_contents, curl and fopen but it depend on your requirement and file permission

php 中有很多方法可以读取文件,如 exec、file_get_contents、curl 和 fopen,但这取决于您的要求和文件权限

Visit this file_get_contents vs cUrl

访问此file_get_contents 与 cUrl

Basically file_get_contents for for you

基本上为您准备的 file_get_contents

$data = file_get_contents($file_url);

回答by Ravi Chauhan

If using Curl in php...

如果在 php 中使用 Curl ...

function disguise_curl($url) 
{ 
  $curl = curl_init(); 

  // Setup headers - I used the same headers from Firefox version 2.0.0.6 
  // below was split up because php.net said the line was too long. :/ 
  $header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,"; 
  $header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"; 
  $header[] = "Cache-Control: max-age=0"; 
  $header[] = "Connection: keep-alive"; 
  $header[] = "Keep-Alive: 300"; 
  $header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7"; 
  $header[] = "Accept-Language: en-us,en;q=0.5"; 
  $header[] = "Pragma: "; // browsers keep this blank. 

  curl_setopt($curl, CURLOPT_URL, $url); 
  curl_setopt($curl, CURLOPT_USERAGENT, 'Googlebot/2.1 (+http://www.google.com/bot.html)'); 
  curl_setopt($curl, CURLOPT_HTTPHEADER, $header); 
  curl_setopt($curl, CURLOPT_REFERER, 'http://www.google.com'); 
  curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate'); 
  curl_setopt($curl, CURLOPT_AUTOREFERER, true); 
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
  curl_setopt($curl, CURLOPT_TIMEOUT, 10); 

  $html = curl_exec($curl); // execute the curl command 
  curl_close($curl); // close the connection 

  return $html; // and finally, return $html 
} 

// uses the function and displays the text off the website 
$text = disguise_curl($url); 
echo $text; 
?> 

回答by Limon Monte

Shellwrapis great tool for using the command-line in PHP!

Shellwrap是在 PHP 中使用命令行的好工具!

Your example can be done quite easy and readable:

您的示例可以非常简单易读:

use MrRio\ShellWrap as sh;

$xml = (string)sh::curl(['u' => 'user:pass'], 'http://example.com/file.xml');

回答by Austin Kregel

This method is only one class and doesn't require importing other libraries or reusing code.

该方法只是一个类,不需要导入其他库或重用代码。

Personally I use this script that I made a while ago. Located herebut for those who don't want to click on that link you can view it below. It lets the developer use the static method HTTP::GET($url, $options)to use the get method in curl while being able to pass through custom curl options. You can also use HTTP::POST($url, $options)but I hardly use that method.

我个人使用我不久前制作的这个脚本。位于此处,但对于那些不想单击该链接的人,您可以在下面查看。它允许开发人员使用静态方法HTTP::GET($url, $options)在 curl 中使用 get 方法,同时能够传递自定义 curl 选项。您也可以使用,HTTP::POST($url, $options)但我几乎不使用该方法。

/**
  *  echo HTTP::POST('http://accounts.kbcomp.co',
  *      array(
  *            'user_name'=>'[email protected]',
  *            'user_password'=>'demo1234'
  *      )
  *  );
  *  OR
  *  echo HTTP::GET('http://api.austinkregel.com/colors/E64B3B/1');
  *                  
  */

class HTTP{
   public static function GET($url,Array $options=array()){
    $ch = curl_init();
    if(count($options>0)){
       curl_setopt_array($ch, $options);
       curl_setopt($ch, CURLOPT_URL, $url);
       $json = curl_exec($ch);
       curl_close($ch);
       return $json;
     }
   }
   public static function POST($url, $postfields, $options = null){
       $ch = curl_init();
       $options = array(
          CURLOPT_URL=>$url,
          CURLOPT_RETURNTRANSFER => TRUE,
          CURLOPT_POSTFIELDS => $postfields,
          CURLOPT_HEADER => true
          //CURLOPT_HTTPHEADER, array('Content-Type:application/json')
          ); 
       if(count($options>0)){
           curl_setopt_array($ch, $options);
       }
       $json = curl_exec($ch);
       curl_close($ch);
       return $json;
   }
}

回答by Max Muster

I understand you want to open a xml file using php. That's called to parse a xml file. The best reference is here.

我知道您想使用 php 打开一个 xml 文件。这被称为解析 xml 文件。最好的参考在这里。

http://php.net/manual/de/function.xml-parse.php

http://php.net/manual/de/function.xml-parse.php

回答by vinod

To run wget command in PHP you have to do following steps :

1) Allow apache server to use wget command by adding it in sudoers list.

2) Check "exec" function enabled or exist in your PHP config.

3) Run "exec" command as root user i.e. sudo user

Below code sample as per ubuntu machine

要在 PHP 中运行 wget 命令,您必须执行以下步骤:

1) 通过在 sudoers 列表中添加 wget 命令,允许 apache 服务器使用 wget 命令。

2)检查“exec”功能已启用或存在于您的PHP配置中。

3)以root用户即sudo用户运行“exec”命令

下面是根据 ubuntu 机器的代码示例

#Add apache in sudoers list to use wget command
~$ sudo nano /etc/sudoers
#add below line in the sudoers file
www-data ALL=(ALL) NOPASSWD: /usr/bin/wget


##Now in PHP file run wget command as 
exec("/usr/bin/sudo wget -P PATH_WHERE_WANT_TO_PLACE_FILE URL_OF_FILE");

回答by Seyyed AMir

<?php
function wget($address,$filename)
{
  file_put_contents($filename,file_get_contents($address));
}
?>

use:

用:

<?php
wget(URL, FileName);
?>