简体   繁体   中英

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:

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

How can I run curl as shell command line?

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

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

shell_exec tells the safe mode problem, but exec just tell you an wrong message, hopefully I tried both exec and shell_exec .

Disable safe mode in your php.ini file. 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.

Then double check that safe_mode = Off is in your php.ini.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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