Howto run a php file from a other php file? - php

Is it possible to launch a php file from a other php file?
I know that i can include a file but i don't mean this.
Small example:
i have a script which displays something from the database
and a other script which get the latest data from a other site and update the database.
When i include the update file, the first script will request the data from the server but i want to make it parallel so that only the update script request from server

Yes, and you can use include provided fopen wrappers are turned on.
include("http://otherserv.com/path/to/script.php");
If you don't care about the reponse from the other server, you can do
get_headers("http://otherserv.com/path/to/script.php");
This will complete much faster if the remote script takes time to process.

Yes, you need CURL (or any other HTTP library) to "run" a php file from another as you described in your scenario.
So, in your case:
CLIENT will run a REQUEST to SERVER1 (a.php)
SERVER1's a.php, will use CLIENT REQUEST to perform a remote PHP file in SERVER2 (b.php)
SERVER2's b.php will process SERVER1's REQUEST and place an appropriate response
SERVER1's a.php will receive SERVER2's b.php RESPONSE, parse it, and send appropriate response to CLIENT.
Hope it clarifies something to you.

you should look at exec http://au.php.net/manual/en/book.exec.php

Related

Sending data from a client to a server by means of a http post

I'm trying to send data from the client to the server. The client runs a simple python script that uses the 'request' library. The server side consists of another simple php script using the $_POST.
I need the webpage to update depending on the data that is given through the client program.
Here is the python script:
import requests
url = "http://xxxxxxx.com/php_files/text_data.php"
d = {'test': 'It works!'}
r = requests.post(url, data = d)
print r.status_code, r.reason
print r.text
And here is the php code:
<!DOCTYPE = html>
<html>
<head>
<h1>
<?php
$txt = $_POST['test'];
echo $txt;
?>
</h1>
</head>
</html>
I need the php page to display 'It works!' on h1 as this is the value that is being passed.
But for some reason, it does not display anything
r.text prints the required format with 'It works!' in the < h1 > tags, but the same does not get displayed in the actual website.
I've also tried var_dump($txt). It gives me a NULL value.
Any help would be gladly appreciated.
It seems to me that you are asking a separate instance to update your current instance. The PHP that you are accessing in your browser knows nothing about the python script. It doesn't call the python script at all. In the second session the python script calls the PHP and receives the correct response.
These are two different sessions, the browser window will see nothing from the python script unless it calls it.
Here is what is happening:
Session 1
Run Python script on local machine
Python calls PHP on server
PHP returns output to local machine
Python prints result
Session 2
Open web browser on local machine
Web browser calls PHP on server
PHP returns results to web browser
Web browser displays results
There is no persistence in the first session to save the information for the second session. They are two completely separate actions. A more typical way would be to set up a database (or just quick and dirty a text file) on the server to save the information. You need to create a second PHP file to save the information to a database or text file on the server. You then need to modify your previous PHP file to read the information from the database or the text file. The sessions would then be set up the following way.
Session 1
Run Python script on local machine
Python calls PHP (new file) on server
PHP writes information from python script to database (or text file)
PHP returns status message to local machine
Python prints status
Session 2
Open web browser on local machine
Web browser calls PHP (original file) on server
PHP reads desired information from database (or text file)
PHP displays information read from server on web browser
Web browser displays results
If you really want to use the results from the python script in the PHP without a database or text file, you will need to upload the python script to your server, and use one of the methods suggested in Calling Python in PHP

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

Calling Jar From Php (What if multiple requests made at the same time?)

I am calling a Jar file on my php server via
exec('java -jar /pat/to/file.jar', $output);
But i am wondering, If two consequent requests made to the php function that runs the jar file, would the second request be able to reach the jar file? Or would it have to wait because the jar file is already in use?
Thanks in advance.
I have used this in the past and it seems to work well: http://php-java-bridge.sourceforge.net/doc/how_it_works.php

Calling php code from php code on different server

The situation is following. I have a local (development) PHP server (Win + IIS) on my computer and I would like to call a function on a remote server because there is an executable file on the remote server (linux) that does not run on my local computer.
So on the remote server would be a simple PHP page which calls the executable with given parameters and returns the results.
I can imagine making an ajax call from javascript to this remote PHP page, but in my case I am using PHPUnit locally and at one point I need to call this remote page for specific data (which the executable file provides).
So my question is - how to make a call from PHP (local) to PHP (remote)? Is web-services the way to go or can this be accomplished simpler?
$output = file_get_contents('http://your.remote.server/page.php?args=12345');
Or you can use cURL. Keep in mind that when you're executing files in PHP you should limit the ability for other people to do so(ie. password protect the page/directory) and escape the shell arguments before running them.
$param['one'] = 1;
$param['two'] = 2;
file_get_contents("http://example.com/service.php?".http_build_query($param));
You can use the PHP cURL library to query scripts on remote servers.
Use fopen or curl to call your remote script.

Determining URL of script that is calling from a remote CURL request

I have a homebase script, that I have many other scripts ping for information using the CURL method. I need to determine the domain name of the callers. Can I do this with tricks just on my homebase script?
. using php .
Hudson
You could send a custom HTTP header with your CURL request that contains the script name, something like
X-SCRIPT-NAME myscript.php
I don't think CURL automatically adds something about the calling script, so you would have to edit the scripts for this.

Categories