Welcome Guest, Not a member yet? Register   Sign In
Asynchronous PHP <-> PHP Calls
#1

[eluser]Réjôme[/eluser]
Hello everyone,

I run into a bit tricky problem :

Let's say there are 2 PHP servers, each one running it's own CI.
One is the Front Office server, open for everyone on the web.
The other is the Back Office server, accepting only Front Office calls (ip filter).

The two servers share a database.
The Front Office feeds the database with user inputs, the Back Office processes the data and takes care of slow treatments.

What I want to do is to trigger Back Office scripts execution asynchronously.
Actually, the Front Office doesn't need to know the result of the Back Office processes. It only has to wake him up.

For portability reasons, I'd like to avoid the usage of cURL or PEAR (or anything that needs special software configuration)

I thought about building a CI Library that could implement fonctions to do it using sockets... Could it be possible and how ?

Thanks for your help !
#2

[eluser]rogierb[/eluser]
You can use sockets or you could use ajax.
Ajax is by far the easiest way. As for sockets, there is a lot of info on how to use fsockopen, fgets and fputs
#3

[eluser]davidbehler[/eluser]
What you want is propably something like this:
Code:
$query_string = "key1=1234&key2=5678";

$request  = "GET http://www.example.com HTTP/1.0\r\n";
$request .= "Host: example.co\r\n";
$request .= "Content-Type: application/x-www-form-urlencoded; charset=utf-8\r\n";
$request .= "Content-Length: ".strlen($query_string)."\r\n";
$request .= "\r\n";
$request .= "$query_string";

$response = '';
$errno = ''; // Contains the error number if something goes wrong with the connect
$errstr = ''; // Contains the error string if something goes wrong with the connect
$timeout = 3; // in seconds

if(FALSE !== ($socket = @fsockopen('example.com', 80, $errno, $errstr, $timeout)))
{
    fwrite($socket, $request);
    fclose($socket);
    return TRUE;
}
else
{
    return FALSE;
}
#4

[eluser]Réjôme[/eluser]
Thanks for your quick replies !

The example using fsockopen() seems to fit my needs.

I initially search for something simpler.
The socket is a very "low level" method just to make a basic http call.
But if this works, I'm fully satisfied !

I'll test it as soon as possible.
#5

[eluser]Réjôme[/eluser]
It works !
Thanks for your help !




Theme © iAndrew 2016 - Forum software by © MyBB