cURL doesn't work from command line? - php

I wrote a script to parse some data from a website using cURL and it works fine when I run it in my browser, however when I want to run it in the command line I get the error "call to undefined function curl_init()". Do php scripts run under different settings from the command line?

This is happening because you are simply trying to call a PHP function from bash. If you have curl installed in your linux environment then the command should simply be curl [-options] [url]. The simplest of them being something like:
$ curl http://someurl.com/path/to/xmlfile.xml
You can test for this from the command line by tying "$ which curl" (without the quotes of course). That will give you the path to where it is stored in case you have to use the full path. (e.g. /usr/bin/curl [-options] [url]).
EDIT:
after having re-read your question I realized that I dumbly missed the fact that you said you were trying to run the PHP script from the command line and not curl itself. And now I, too, am stumped by your problem. Sorry!

Related

Why does wget on the command line work, but file_get_contents does not?

I am trying to download the contents of an html file to my Ubuntu Linux 16.04 computer using php's get_file_contents() function. However, when I do this, I get this Warning: "failed to open stream: the , aborting"
Yet when I use wget on the terminal command line, it quickly downloads the file contents.
So why does file_get_contents not work for this? Here is my php code, which produces the Warning:
$testDownload = file_get_contents("https://ebird.org/region/US-AL-001?yr=all");
echo $testDownload;
On my Ubuntu terminal command line, here is my bash code, which works quickly and flawlessly:
wget https://ebird.org/region/US-AL-001?yr=all
I want to use php because I want to automate the downloading of a number of files and need a fair bit of code to do it, and I feel much more comfortable using php than bash.
P.S. I tried various "context" solutions for the file_get_contents function that were suggested on Stack Overflow, but they did not solve the problem.
P.P.S. I earlier tried cURL and got the same redirects Warning, though I admit to not knowing much about cURL.
I found a solution: the shell_exec() function in php allows me to use bash's wget command line function within my php script. I tried it and it worked. (I will have to change the ownership of the downloaded files to get access to them.) Here is the code that worked:
$output = shell_exec('wget https://ebird.org/region/US-AL-005?yr=all');
I still don't understand why wget can get the file contents but file_get_contents cannot. But with shell_exec() I have found a php solution to complete my task, so I am happy.

php from command line empty

I have updated windows today and now when I use the command line and type PHP nothing returns. tried various PHP commands, nothing, not even an error or response. blank.
I had the correct path in the environment variables. Tried to change to another PHP version and even tried removing the path from environment variables but still returns empty!
It doesn't even say that "PHP" is not recognized although I removed the environment variable!!
I have no clue how to solve this.
If you are not passing extra parameters when you call it from command line, that´s how it supposed to work.
Try running:
php --help (to see all options from command line)
Also, to make sure it is running try to do this:
php -v (to check version)
php -F filename.php
The last one should run your php file.
I had the same challenge.
Here's a solution:
Use a standard terminal window (instead of Powershell).
Try php -v again.
IF you get an error mentioning VCRUNTIME140.dll
THEN you need to install run-time components that are required to run C++ applications built using Visual Studio 2015 here: https://www.microsoft.com/en-us/download/details.aspx?id=48145

Running PHP script on Perl or sh error. Not found

I'm currently running a cron job that loads a php script.
I keep getting an error, sh 1 /usr/bin/php: not found.
I tried it two other ways but to no avail.
on a perl script. I tried.
my $x = qx('/usr/bin/php /home/script.here');
This doesn't generate anything and sends me an error message on my mail.
But if I run the line
/usr/bin/php /home/script.here
on my shell, it works.
I also create a script 1.sh and had this.
#!/usr/bin/php -v
I run the script ./1.sh and it shows the result. But as soon as I try to call it via cron or /bin/sh 1.sh, it just fails and can't find the php path even if it was explicitly stated.
Am I missing anything?
I also tried this on php5, but same error.
The problem are the single quotes inside the qx() operator. Remove them:
my $x = qx(/usr/bin/php /home/script.here);
As long as they are there the shell tries to find a command "script.here" in the directory "/usr/bin/php /home" (yes, with the space in the directory name).
Totally forgot about this question.
Found a solution.
I just added
SHELL=/bin/bash in crontab and the scripts worked.

php environment variable is not working

I've followed tutorials and answer here on Stackoverflow, but I still can't access php.exe from command line.
All I get is:
php is not recognized as an internal or external command...
How can I make php.exe accessible from anywhere in the command line?
What else can I try to execute PHP from command line?
Path should be
C:\wamp\bin\php\php5.4.12
without php at end

php exec() is not executing the command

I have tried to use exec() with 'whoami' to check if it works and I got the result of
nt authority\system
Now I need to run a .exe file with parameters from php via exec() function.
I tried this in command prompt and it actually runs the program with given parameters. This is the example command.
NOTE the exe file gets 3 inputs (folder, file_name, report_file_nmae)
> ..\..\some_file.exe folder="C:\path_to_folder" param=1.xml report=2.xml
But when I run this command from php file:
exec('..\..\some_file.exe folder="C:\path_to_folder" param=1.xml report=2.xml');
nothing is happening. This is the first time I am using exec() function, so I am not familiar with its details. What is wrong?
I tried using:
\\ instead of \
escapeshellarg() on the directory
added "" around directory folder names
No luck
Addendum:
echo exec($command) // echos < .... why?
or
exec($command, $output);
print_r($output); // Array()
I even changed the permission on the file to full control to all users.
If I call the program from command prompt, I can see the icon appearing next to clock for a second.
But the same call from php will not even call the program.
Edit
Even exec('notepad.exe'); is not working. Something has to be done with php configurations maybe?
I already said that I was new to exec() function. After doing some more digging, I came upon 2>&1 which needs to be added at the end of command in exec().
Thanks #mattosmat for pointing it out in the comments too. I did not try this at once because you said it is a Linux command, I am on Windows.
So, what I have discovered, the command is actually executing in the back-end. That is why I could not see it actually running, which I was expecting to happen.
For all of you, who had similar problem, my advise is to use that command. It will point out all the errors and also tell you info/details about execution.
exec('some_command 2>&1', $output);
print_r($output); // to see the response to your command
Thanks for all the help guys, I appreciate it ;)
You might also try giving the full path to the binary you're trying to run. That solved my problem when trying to use ImageMagick.

Categories