Calling a PHP page with GET variables from the Linux command line - php

I want to use wkhtmltopdf to convert a web page to a PDF file. I have a test with a static template and this syntax works perfectly:
wkhtmltopdf my.html my.pdf
The problem is the actual page is a dynamic PHP page with tables that rely on three HTML GET variables.
An example would be:
http://mypage.php?clientid=SJC&datestart=201201&dateend=201202
I can't do this directly like so:
wkhtmltopdf mypage.php?clientid=SJC&datestart=201201&dateend=201202 my.pdf
Someone suggested I needed to call the PHP from the command line with the variables first to get the HTML source code for that set of variables, and convert it using wkhtmltopdf.
How do I do this? What is the process using the above URL as an example?

The cleanest way would be using $_SERVER['argv'] instead of the GET variables.
However, if you HAVE to use the GET variables, you can set them in a custom script:
$_REQUEST['var1'] = $_SERVER['argv'][0];
and then require() the PHP script itself.
Another way would be to set the environment variables QUERY_STRING and REQUEST_METHOD:
export REQUEST_METHOD=GET
exprt QUERY_STRING='var1=blub&var2=blah'

In Linux you can use the wget command to get a result HTML file from an URL:
wget "http://localhost/mypage.php?clientid=SJC&datestart=201201&dateend=201202"
Or
wget -O myfile.html "http://localhost/mypage.php?clientid=SJC&datestart=201201&dateend=201202"
to output the result to specific file, for example, myfile.html
Note:
wget wget -O myfile.html "http://localhost/mypage.php?clientid=SJC&datestart=201201&dateend=201202"
Double quote seems to solve the ampersand encoding problem.

Related

Call to wget from PHP's shell_exec not working

I'm trying to run a PHP script locally that scrapes Google with wget and dumps the HTML into temp.html.
Running this command from the terminal works fine:
wget -O temp.html http://www.google.ca
Running this command from PHP also works fine (so it's not a permissions issue):
shell_exec('touch temp.html');
But running this from PHP does not work (does not create temp.html):
shell_exec('wget -O temp.html http://www.google.ca');
Any suggestions? Wrapping that last command in a var_dump() outputs null.
Thanks!
According to man wget, using wget -O temp.html http://google.com takes all documents, concatenates them and prints everything in temp.html, without producing any stdout so PHP's shell_exec doesn't return anything (null).
The content of the scraped webpage should be present in temp.html, but shell_exec("wget ...") does not return anything, as not output is produced.
As you mentioned the webpage you are trying to scrape does not work, maybe they implemented some sort of bot-protection preventing exactly what you are trying.
Edit: You may use - to print everything to stdout instead. So try using shell_exec("wget -O - https://google.com"); should return the content of the requested page to your PHP script.
The simplest solution is to provide full path to the wget binary as it seems the user that runs your script does ot have the same $PATH as you.
How about using file_put_contents & file_get_contents instead? This should work without having to worry about wget.
<?php
$filename = 'temp.html';
$address = 'http://www.google.ca';
file_put_contents($filename,file_get_contents($address));
?>

How to use exec method to start exe application and pass arguments in PHP?

It sounds very simple but I must be missing something here. I have a custom exe program, which is inside C:\dummy\dummytest.exe and I have a text file inside C:\text\test.txt . All I want to do is start dummytest.exe by passing test.txt as argument in PHP. Here is what I tried:
$arg = "C:\text\test.txt"
exec("C:\dummy\dummytest.exe".$arg);
I tried with just single '\' also. And I tried
exec("C:\dummy\dummytest.exe $arg"); but nothing seems to work. I get C:\dummy is not recognized as an internal or external command, operable or batch file error.
When I go to command line manually and do
C:\dummy\dummytest.exe test.txt
the application runs just fine. What am I missing here with exec?
Use the "shell_exec" command instead.
shell_exec("[BAT or EXE-File] [Params]");
Hope this helps!
EDIT
When using executables and parameters with paths you have to quote them.
So, an example would look like this:
echo nl2br(shell_exec("\"F:\\N3V Games\\Trainz Simulator 12\\compile_gs.bat\" \"F:\\xampp\\htdocs\\SHELL\\EBuLa.gs\""));
This example prints the output of a CMD-Window directly to the page.
If the executable is placed in the same directory like the php-file you can just run:
echo nl2br(shell_exec("compile_gs.bat EBuLa.gs"));

A better way of putting allot of information into an SQL database, than querying in URL

I have a simple php script set up, through which I put information into an MySQL database by the following URL query:
https://www.thesite.com/addinformation.php?WHERE_TO_LABEL_IT_AS&WHAT_TEXT_TO_ADD"
I call it from bash by simply using wget
wget --no-check-certificate --user=username --password='password' --delete-after https://www.thesite.com/addinformation.php?$LABELVARIABLE&$STRINGVARIABLE"
It works for my needs, but I would now like to use a similar way to add large chunks of text into the database. Perhaps even whole text files. What method would you recommend I read up on to do this?
You could save your content to a file, and post it using either wget or curl.
$ wget --post-file=<file path>
or
$ curl --data-binary #<file path>

Compile C++ file Using PHP

I am using PHP on Windows machin. I also use Dev C++. I can perfectly compile .cpp file on CMD using this command:
g++ hello.cpp -O3 -o hello.exe
Now what I am trying to do is running the same command using php system() function, so it looks like this:
system("g++ c:\wamp\www\grader\hello.cpp -O3 -o C:\wamp\www\grader\hello.exe");
but it doesn't compile. I am lost, please tell me what am I missing?
I also looked up at this question and thats exactly what I need, but I couldnt find a usefull solution for my case there:
Php script to compile c++ file and run the executable file with input file
Use the PHP exec command.
echo exec('g++ hello.cpp -O3 -o hello.exe');
should work.
There's a whole family of different exec & system commands in PHP, see here:
http://www.php.net/manual/en/ref.exec.php
If you want the output into a variable, then use :
$variable = exec('g++ hello.cpp -O3 -o hello.exe');
If that doesn't work, then make sure that g++ is available in your path, and that your logged in with sufficient enough privliges to allow it to execute.
You may find also that it's failing beacuse PHP is essentially being executed by your web server (Unless your also running PHP from the cmd prompt) , and the web server user ID may not have write access to the folder where G++ is trying to create the output file.
Temporarily granting write access to 'Everybody' on the output folder will verify if that is the case.
Two things:
You are using double quotes and are not escaping the \ inside the path.
You are not using a full path to g++.
The first one is important as \ followed by something has a special meaning in such a string (you might know \n as new line), the second one is relevant since the PHP environment might have a different search path.
A solution might be
system("c:\\path\\to\\g++ c:\\wamp\\www\\grader\\hello.cpp -O3 -o C:\\wamp\\www\\grader\\hello.exe");
Alternatively you can use single quotes, intead of double quotes, they use diffeent,less strict escaping rules
system('c:\path\to\g++ c:\wamp\www\grader\hello.cpp -O3 -o C:\wamp\www\grader\hello.exe');
or use / instead of \, which is supported by windows, too.
system("c:/path/to/g++ c:/wamp/www/grader/hello.cpp -O3 -o C:/wamp/www/grader/hello.exe");
What you do is your choice, while many might consider the first one as ugly, and the last one as bad style on Windows ;-)
Thanks to everyone. I tried to run the codes given in above posts and it worked like a charm.
I ran the following code using my browser
$var = exec("g++ C:/wamp/www/cpp/hello.cpp -O3 -o C:/wamp/www/cpp/hello.exe");
echo $var;
The exe file is created. I am able to see the result when i run the exe file but the problem is when i run the above code in the browser, the result is not displayed on the webpage. I gave full access permission to all users but still give does not show the result on the webpage.
I really need help on this since i am doing a project on simulated annealing where i want to get the result from compiled c++ program and display it in the webpage with some jquery highcharts.
Thanks again to all, it has helped me alot and i have learnt alot as well.

Cronjob using CURL/WGET

I want to run a PHP script every 15 minutes using either CURL or WGET.
This PHP file is in a local folder:
/home/x/cron.php
How would I run this using CURL/WGET?
It doesn't work when I try to run
curl /home/x/cron.php
Thank you!
CURL and WGET are more adecuate for URLs like http://myhost.com/cron.php
When the script is offline, you would better run it using php CLI:
Ex:
php -q cron.php
Just do something like this:
/usr/bin/php /home/x/cron.php
cURL/wget is for HTTP actions. If your PHP script is on the same system, you don't want to load it over HTTP. (You can, of course, if it is accessible over HTTP, but I don't think that is what you want.) Just call it directly.
Alternatively, you can set the execute permission on your script and throw in a shebang line for PHP:
#!/usr/bin/php
Then, just put your PHP script in crontab directly.
If you're using CURL or WGET, I believe you'll need to pass in the path as a URL. If you want to run the php script on the command line, you'll need to use the the php CLI

Categories