简体   繁体   English

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

[英]How can I run curl as shell command line in PHP

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上。

The issue is that PHP safe mode is on and it is better to use the full path to run cURL (thanks ghostJago and amosrivera). 问题是PHP安全模式已启用,最好使用完整路径来运行cURL (感谢ghostJago和amosrivera)。 Running the script with the following command fixed the issue: 使用以下命令运行脚本可修复此问题:

php -dsafe_mode=Off test.php

I do not want to change the php.ini but it could be a solution too. 我不想更改php.ini但它也可能是一个解决方案。

shell_exec tells the safe mode problem, but exec just tell you an wrong message, hopefully I tried both exec and shell_exec . shell_exec告诉安全模式问题,但exec只是告诉你一个错误的消息,希望我尝试了execshell_exec

Disable safe mode in your php.ini file. 禁用php.ini文件中的safe mode Also check if you do have curl installed. 还要检查是否安装了卷曲。

safe_mode = Off

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

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 = Off is in your php.ini. 然后仔细检查你的php.ini中是否有safe_mode = Off

When you've done this, change your code to: 完成后,将代码更改为:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM