Get cURL command line given PHP cURL code - php

Given a PHP source code with curl handle, how do I get the command line version of that curl request?

Look at the cURL documentation here:
http://curl.haxx.se/docs/manpage.html
or, go to your terminal and type:
man curl
After reading the documentation, you will have to manually find out which commands map to which functions to get the results you want. Unlikely to be any easier way to do this.
A lot of it will be just looking at which of PHP's curl_setopt() parameters map to the matching command line parameters.

You could just create a php file with the curl command in it, and then just run the php script from the command line.
hostname$ php curldownload.php
Or, you could have a look here for examples: http://www.thegeekstuff.com/2012/04/curl-examples/

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

curl command line works, php shell_exec() does not

There are several other posts in reference to this problem. The difference here is that I am willing to give out the troublesome url.
This works:
curl https://pas-gdl.overdrive.com/advanced-search
This does not work:
$pagesource = shell_exec("curl https://pas-gdl.overdrive.com/advanced-search");
I get the dreaded 51 error: "curl: (51) SSL: no alternative certificate subject name matches target host name 'pas-gdl.lib.overdrive.com'"
There is a wildcard ssl cert involved. I have attempted to figure out what the command line curl is doing by default as a possible solution. However, seeing as I am just executing the same command via shell_exec there should be zero difference.
The command line option produces the advanced-search html and the shell_exec does not. Any information as to why would be greatly appreciated.
use php curl instead
also look into how to curl https

execute php via cli passing arguments

how do I execute via command line a php script passing get paramateres. I have been trying to execute the following
chmod +x /media/linkstation/myfolder/import/import.php
sudo php5 ./media/linkstation/myfolder/import/import.php?id=4
but won't work Could not open input file
You are trying to mix file protocol with http protocol, that won't work.
The parameters need to be separated from the file path.
Edit:
As long as the path is correct, you can try:
sudo php5 ./media/linkstation/myfolder/import/import.php id=4
Now the first arg $argv[1] is the query string, so long as that is a single string (or wrapped in quotations) it will appear on the receiving end of the script. After that you can treat it as you like.
However there are more suitable ways to delivery parameters. You may find command line options more useful: http://php.net/manual/en/function.getopt.php
sudo php5 ./media/linkstation/myfolder/import/import.php --id="4"

cURL doesn't work from command line?

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!

Categories