【问题标题】:PHP shell_exec get filePHP shell_exec 获取文件
【发布时间】:2021-09-21 06:37:34
【问题描述】:

有没有办法获取外部网站的文件内容,比如https://www.example.com/。 例如。使用来自 shell_exec 的 Python。我尝试了 python,但得到了空白。

【问题讨论】:

  • 您是否尝试从远程网络服务器上抓取源文件?

标签: php shell-exec


【解决方案1】:

不知道为什么要使用 shell_exec 但是 你可以使用

$data = file_get_contents('https://www.example.com/');

$url = 'https://www.example.com/';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
$data = curl_exec($curl);
curl_close($curl);

如果你想保持会话,你可以使用 curl 和 CURLOPT_COOKIEFILE 例如:

$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookiefile");
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookiefile");
curl_setopt($ch, CURLOPT_COOKIE, session_name() . '=' . session_id());
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

curl_setopt($ch, CURLOPT_URL, 'https://www.example.com/');
$data = curl_exec($ch);

或者如果你真的想使用 shell_exec,使用下面的方法

$data = shell_exec('curl -I https://www.example.com/');
echo "<pre>$data</pre>";

但是,要使上述工作正常,您必须安装 curl 检查以下是否安装 curl https://curl.se/

希望这可行

【讨论】:

  • 好的,非常感谢。
猜你喜欢
  • 2011-08-08
  • 2017-06-29
  • 2012-09-05
  • 1970-01-01
  • 2016-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-16
相关资源
最近更新 更多