Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I run curl as shell command line in PHP

Tags:

php

curl

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.

like image 926
rtacconi Avatar asked Sep 27 '11 15:09

rtacconi


People also ask

Can we use curl command in shell script?

The curl command transfers data to or from a network server, using one of the supported protocols (HTTP, HTTPS, FTP, FTPS, SCP, SFTP, TFTP, DICT, TELNET, LDAP or FILE). It is designed to work without user interaction, so it is ideal for use in a shell script.

How do I run a shell script in PHP?

The shell_exec() function is an inbuilt function in PHP which is used to execute the commands via shell and return the complete output as a string. The shell_exec is an alias for the backtick operator, for those used to *nix.

Can I use curl in PHP?

Uses of cURL in PHPcURL is a PHP extension that allows you to use the URL syntax to receive and submit data. cURL makes it simple to connect between various websites and domains. Obtaining a copy of a website's material. Submission of forms automatically, authentication and cookie use.


2 Answers

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.

like image 107
rtacconi Avatar answered Oct 19 '22 23:10

rtacconi


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

safe_mode = Off
like image 22
amosrivera Avatar answered Oct 20 '22 01:10

amosrivera