Calling php code from php code on different server - php

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.

Related

PHP request on self

I experience problem with PHP request on self. In example I will use file_get_contents() but same happen for exec('wkhtmltopdf [*SELF*]') or curl()
lets name my server example.com
apache2 installed
FastCGI (multiple PHP versions 5.3, 5.4, 5.5, 5.6, 7.0)
now I have 2 dummy scripts
1st script
//get-html.php
file_get_contents('http://example.org/index.html')
2nd script
//get-php.php
file_get_contents('http://example.org/index.php')
Testing
1) command-line: php get-html.php // Success
2) browser: example.org/get-html.php // Success
1) command-line: php get-php.php // Success
2) browser: example.org/get-php.php // Timeout
What I tried next
create subdomain like subdomain.example.org/index.php to have differet PHP version for get-php.php and for index.php
amend /etc/hosts
request on other sites (like google.com) // Success
session_write_close() before file_get_contents() and session_start() right after does not work also
So my suspect is mod_fastcgi. It seems like the apache is not able to run 2 instances of this to handle PHP requests which comes from itself. As running script from command line works as expected.
Does anybody have any advice?
I did not set PHP_FCGI_CHILDREN what is in default 1.
When I called in PHP script another PHP script from my server over apache it failed as it was not able to create another PHP FCGI instance.

Bottle web server - how to serve PHP file?

I am working on a webapp made by someone else which uses Bottle routing. I want to create a simple login page which requires some PHP. If I return the PHP page as a static_file, any HTML will be executed but PHP won't, for obvious reasons. How should I serve the PHP file so that it is dynamic?
Not working:
#route('/login')
def serve():
return static_file('login.php', root='.')
In order to server PHP files, you need to have PHP installed on the web server. Additionally, the webserver needs to be configured to detect PHP files and execute them.
Serving PHP files from Python is kinda useless and not recommended.
I'd recommend you to take the time to translate this script from PHP to Python.
I wanted to do the same thing yesterday, but the answers I got to my question made it clear it was either impossible or extremely difficult. I came up with writing a small python program to run the PHP built in server. NOTE: PHP needs to be able to run from the command line for this to work.
#Import the os package so that this code can run commands
import os
#Get the port that the user wants to host on
port = str(input("What port would you like to host on?"))
#Add wanted port to the command that hosts the php server
cmd = "php -S localhost:" + port
#Actually run the command to host php server
os.system(cmd)
#Now the PHP server will take over until you
#use ctrl + C to quit hosting
Just remember that the port needs to be 4 numbers. When you host this, you can return any file from the folder you ran this code in by simply typing it in the browser. Example:
localhost:8080/login.php
Returns login.php (if it is there) on the localhost port that you asked for.

exec() disabled, how to execute .jar file

PHP's exec() function is turned off in my hosting server and it's not possible to change it (for security reasons). Are there alternatives for the exec() function?
Here is more info about my problem:
What I want to do: I want to use .jar file in host server.
This is how it looks on my localhost:
function generateReqXML() {
exec('"C:\Program Files\Java\jdk1.7.0_21\bin\java.exe"
-jar vaiisis.jar auth', $out);
}
This .jar file help my system to generate XML code.
This code work perfect for my local machine, but its facing the problem when I am trying to use it in my host server.
Alternatives to exec are:
system(); passthru(); shell_exec();
You can check if these are available with echo ini_get("disable_functions");
But since these are probably disabled there are only non-PHP options left.
Do you have to use the exec() function via PHP?
You could for example write a shell script, which can be invoked via cron to do your job.
What are you exactly trying to accomplish?

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.

How do I call a python scipt by php on a less powerful server to be run on an other more powerful server (cluster)?

I have a action.php script from where I call a python script like this:
$call_python = "../python/python myPythonScript.py ".$someArgument;
$python_output = Null; // stores every output generated by myPythonScript.py
$mystring = exec($call_python, $output_python);
I know, this is perhaps not the best way, but it works for me so far. Recently I discovered, that the computational power of the actual server is not sufficient. So I want the python script to be run on a more powerful server (a cluster) which is in the same network.
Is this possible?
You can easily replace your exec() call with ssh2_exec(). To get the output use stream_get_context() however. That's probably the sanest solution here.
You just need to open a connection to your secondary server beforehand. See ssh2_connect and ssh2_auth_password for that. And of course you need the ssh2 PHP extension installed and enabled.

Categories