如何在 PHP 中将 curl 作为 shell 命令行运行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7572078/
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-26 02:58:24  来源:igfitidea点击:

How can I run curl as shell command line in PHP

phpcurl

提问by rtacconi

If I try to run this inside a script:

如果我尝试在脚本中运行它:

<?php exec("curl http://ww.google.com") ?>

I get:

我得到:

-bash-3.2$ php test.php 
sh: /curl: No such file or directory

using shell_exec:

使用 shell_exec:

PHP Warning:  shell_exec(): Cannot execute using backquotes in Safe Mode...

How can I run curl as shell command line?

如何将 curl 作为 shell 命令行运行?

Those errors are happening on Linux, on my mac works.

这些错误发生在 Linux 上,在我的 Mac 上工作。

采纳答案by rtacconi

The issue is that PHPsafe mode is on and it is better to use the full path to run cURL(thanks ghostJago and amosrivera). Running the script with the following command fixed the issue:

问题是PHP安全模式开启,最好使用完整路径运行cURL(感谢ghostJago和amsrivera)。使用以下命令运行脚本修复了问题:

php -dsafe_mode=Off test.php

I do not want to change the php.inibut it could be a solution too.

我不想改变,php.ini但它也可能是一个解决方案。

shell_exectells the safe mode problem, but execjust tell you an wrong message, hopefully I tried both execand shell_exec.

shell_exec告诉安全模式问题,但exec只是告诉你一个错误的信息,希望我同时尝试了execshell_exec

回答by amosrivera

Disable safe modein your php.ini file. Also check if you do have curl installed.

safe mode在 php.ini 文件中禁用。还要检查您是否安装了 curl。

safe_mode = Off

回答by simon

To convert from an bash command (like you can copy from chrome-dev-tools) to php take a look at this: https://incarnate.github.io/curl-to-php/

要将 bash 命令(就像您可以从 chrome-dev-tools 复制)转换为 php,请查看:https: //incarnate.github.io/curl-to-php/

回答by ghostJago

at the commandline, do this:

在命令行中,执行以下操作:

which curl

This will give you the absolute path to the curl program.

这将为您提供 curl 程序的绝对路径。

Then double check that safe_mode = Offis in your php.ini.

然后仔细检查safe_mode = Off您的 php.ini 中的内容。

When you've done this, change your code to:

完成此操作后,将代码更改为:

<?php exec("path/you/got/from/which/curl http://www.google.com") ?>