0

What I essentially want to do is to be able to call a function or script on another server using PHP and receive a response. Let me set up an example:

On my web application, I have a page, and I need to send some data to the server, have the server do some work and then return a response.

Web application:

<?php

// call server script, sending it for example a string, "testString", then wait for a response

?>

Server script:

<?php

// get the string "testString", so some work on it and return it, displaying it on the web app

?>

I'm not looking for someone to complete this for me, just to head me in the right direction. I've read that cURL can be useful for this, but have yet to be able to get any examples to work for me, such as these ones:

http://www.jonasjohn.de/snippets/php/curl-example.htm

How to get response using cURL in PHP

So what's an ideal route to head in to solve my problem?

Community
  • 1
  • 1
Kestami
  • 2,045
  • 3
  • 32
  • 47

2 Answers2

0

If you do not have control over both the machines or the host server does not provide you with an API to be able to do that, this is not doable. Otherwise it is as simple as setting up some code on host server which will receive commands from your client, then process and respond accordingly. Once that is setup then you can easily call your server code with either cURL or even file_get_contents

Munawir
  • 3,346
  • 9
  • 33
  • 51
Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
  • I don't suppose you know of anywhere with examples? the ones i seem to have tried havn't yielded any results. I do have control over both machines though. – Kestami May 14 '13 at 12:01
0

cURL operates just like a browser basically. It makes an HTTP request to a server and gets back the response. So one server is the 'client' and one server is the 'server'.

On the server server (lol), set up a page called index.php that outputs some text

<?php

echo 'hello from the server server';

Then from the client server, create a page called index.php to make the cURL request to the server server.

<?php

// init curl object        
$ch = curl_init();

// define options
$optArray = array(
    CURLOPT_URL => 'http://www.serverserver.com',  <--- edit that URL
    CURLOPT_RETURNTRANSFER => true
);

// apply those options
curl_setopt_array($ch, $optArray);

// execute request and get response
$result = curl_exec($ch);

var_dump($result);

Then, when you go to the client server URL, the client server will hit the server server, just like it was a person using a browser over HTTP, and print the result to the screen.

Hope that helps. Didn't actually test it.

siliconrockstar
  • 3,554
  • 36
  • 33