How to run via the code another php file - php

I have some cron that run, and I have to run the file inside the cron.
now I'm using "file_get_contents();" there is a better way to run this file?
$qq=mysql_query("SELECT file FROM db");
while ($ww=mysql_fetch_assoc($qq)) {
file_get_contents('http://www.domain.com/'.$ww['file']);
}
thanks

Yes. Set a cronjob server-side.
Or open a socket.
http://php.net/manual/en/function.fsockopen.php
it does not slowdown user's interaction

Execute PHP within your cronjob. Don't use HTTP on localhost to do it... you're just adding more to the process.

File get contents is going to run file inline and return the results. If thats what you want then that will work.
If you want to get the string and run it in place instead and have it interact with in the existing document you can use eval
http://php.net/manual/en/function.eval.php

Related

Execute a php command from another php file

I having a simple problem, I guess.
I am working on an iPhone app which I can send ASIHTTPRequest to my php server (with go daddy). The php script then gets the command and run like:
$this->pdo->beginTransaction();
//do some other simple works
exec ('/usr/local/bin/php -f /path/to/my/script/test.php') ;
$this->pdo->commit();
which is suppose to run another php file within my own server (dedicated)!!! But it does NOT do anything. It does work with curl_exec() though, but I want to use another method which I can put it to work in the background server.
My planning was that I want to send too many APNS (notification) but instead of waiting for the whole list to be done, it is better to get back and let the work done in the background!! How can I do that.
When I got connected using SSH command line. I can easily call "test.php" and it works so fine. But I can not do the same thing from the above php code.
Any help is appreciated.
Use PHP Include? That will run the script.
Put something like this inside an IF Statement.
include 'YourPage.php';
My first thought is that GoDaddy might not allow exec() to be run for security purposes.

Make output of cli based PHP script viewable from web without piping to a file?

I have a command line PHP script that runs constantly (infinite loop) on my server in a 'screen' session. The PHP script outputs various lines of data using echo.
What I would like to do is create a PHP web script to interface the command line script so that I can view the echo output without having to SSH into the server.
I had considered writing/piping all of the echo statements to a text file, and then having the web script read the text file. The problem here is that the text file will grow to several megabytes in the space of only a few minutes.
Does anyone know of a more elegant solution?
I think expect_popen will work for you, if you have it available.
Another option is to used named pipes - no disk usage, the reading end has output available as it comes.
The CLI script can write to a file like so:
file_put_contents( '/var/log/cli-log-'.date('YmdHi').'.log', $data );
Thereby a new log file being created every minute to keep the file size down. You can then clean up the directory at that point, deleting previous log files or moving them or whatever you want to do.
Then the web script can read from the current log file like so:
$log = file_get_contents( '/var/log/cli-log-'.date('YmdHi').'.log' );
As Elias Van Ootegem suggested, I would definitely recommend a cron instead of an constantly running script.
If you want to view the data from a web script you can do a few things....one is write the data to a log file or a database so you can pull it out later....I would consider limiting what you output if you there is so much data (if that is a possiblity).
I have a lot of crons email me data, not sure if that would work for you but I figured I would mention it.
The most elegant suggestion I can think of is to run the commands using exec in a web script which will directly output to the browse if you use : http://php.net/manual/en/function.flush.php

How to set CRON using php and run php file with the help of it?

I have two php files. In one php file there is simple html form in which I created some drop down for select time and days for cronjob when user set time and day and submit form then all the drop down values stored in database.
Now with the help of this stored values I need to set cronjob and when cron will execute then it run another php file in which I write some code for generate xml file.
And this file run every time which time is stored by user in database.
By using Cpanel I can do that but I don't want to use cpanel.
Please give me some proper and working solution, I really need it.
Thanks in advance.
Use phps system() or exec() function to call the crontab command to replace or modify the existing crontab for the account the web server runs under. You might have to make sure that user is allowed to use the cron system, this depends on the operating system you use.
From within the crontab you can use one of two strategies to run a php script_
- call the php cli interpreter like any normal command, so something like: /usr/bin/php and give it the script to interpret. As an alternative you also can use shebang inside your php script and call it as a simple executable.
- use wget to call an url pointing to your webserver (maybe localhost) when you want to execute the script inside your web server.
This might act as a starting point for you to experiment with:
#!/usr/bin/php
<?php
// the time tokens to be fed into the crontab line
$time=array('10','*','*','*','*');
// the actual command to be executed
$command=sprintf("crontab -l | (cat;echo \"%s\t%s\") | crontab",
implode(' ',$time),
'/usr/bin/beep');
// execute command
$result=system($command);
// output result of execution
if ($result)
echo "Result: $result\n";
else
echo "FAILURE!\n";
?>
Works for me when called from CLI.

Cronjob doens't understand the code to execute?

I have set up a cronjob which updates a bunch of contracts in a certain system. When I run the PHP-script in a browser it all works fine, but when the cronjob has to do the trick it fails. I'm kinda stuck on this one since I don't have a lot of experience with cronjobs (heck.. I can only set them up in DirectAdmin).
My PHP scripts has some includes to some classes, these includes work properly (i've tested it by sending mails to myself line by line). When the base-classes are included I have a class which handles autoloading. When I do something like Class::GetInstance() it fails.
My cronjob looks like:
* * * * * /usr/local/bin/php /home/username/domains/domain/public_html/path/to/script.php
What can I do to fix this? Perhaps not run it via php, but by a browser or something? I'm sorry if this is a stupid question, but I don't know this ;)
Remeber that when PHP is executed on CLI with /usr/local/bin/php you do not have the $_SERVER variable setted properly! I had that problem too because my script had to use $_SERVER['DOCUMENT_ROOT']. As said, try to run it in a normal shell to see if it works. Alternatively you can change your cronjob command to:
wget -q http://yourdomain.com/path/to/script.php
Usually this works well because it is just identical to fetch that URL from a normal browser.
wget man page here: http://linux.die.net/man/1/wget
You can't always call the php file directly that expects to be called via HTTP. Judging from path, it's a part of website, which is normally executed by browser, hence I'ld set the cronjob up to not to be directly called by php-cli, but rather by doing a curl request to the website's URL.
"it fails" is not the problem description one can call a suffucient one.
add this line in your crontab file
MAILTO=your#mail
and run your jobs.
You will get the script output and be able either to correct your code or ask a sensible question.
You may also redirect stdout and stderr to a log file

Can't run shell script from php web script

I am trying to run a shell script from a php script.
I have complete control of the environment (unix on mac), I should have all the permissions, etc. set correctly.
The web script is in /htdocs/
The shell script can be executed from anywhere so when I go to /htdocs/ in the shell, I can easily run it like this:
$ my_shellscript
.. but when my php script (which is located in htdocs) tries to call it:
shell_exec('my_shellscript');
I get nothing.
I have proven the script can be called from that location and I have temporarily granted full access to try to get it working somehow. I am going crazy, please help.
If you know of some other way of triggering a shell script via the web that would be fine.
Thanks in advance.
well i got few weeks same problem, the solution is to check if the apace has the permission to execute your script. You could also try to run the script in php cli.
Since it is a shellscript, it needs to be invoked with the path prefix. My guess is you need to do this:
shell_exec('./my_shellscript');
First thing: make sure php isn't running in Safe Mode
Next thing: Try running it with the exec() function and using the full path (e.g. /var/www/htdocs/my_shellscript)
Try doing
echo shell_exec('my_shellscript 2>&1');
which will capture the script's stderr output and print it out. If something inside the script is failing, this output would otherwise be lost when not being run interactively.

Categories