View Full Version : Get value of a function on a remote server

09-02-2009, 02:35 PM
Hey everyone,

This is something that I've been struggling with for a while.

Lets say I have a file on the web : http://www.example.com/function.php

That file has a function that does some stuff depending on what is sent to the function and then returns an array.

My question :

How can I call this function from my local php installation?

09-02-2009, 02:42 PM
I think you would have to make a copy of that file on your local server to be able to use the functions inside it. I don't think you'd be able to use PHP functions from other servers?

09-02-2009, 02:57 PM
How can I call this function from my local php installation?

you cannot call a function on some other server from your local php installation, but you can call that file, and see what it outputs.

there are several ways to communicate with remote servers and maybe one of the most popular ones is cURL (http://php.net/manual/en/intro.curl.php)

09-02-2009, 03:03 PM
Thanks for the replies guys.

I'm not so clued up on curl, I'm going to have a look into it now.

The catch here is that the function file needs to communicate with a database that is online and a few XML files too.

So somehow I have to get that functions's output to my local machine.

09-02-2009, 03:20 PM
So somehow I have to get that functions's output to my local machine.

cURL is good because with it you can send additional parameters to online script like post or get values.

your script can grab output from remote server few ways.

even this simple one

$output = file_get_contents("http://www.example.com/testscript.php");
print $output;

if testscript.php on example.com is

print "output";

will fill your $output local variable with testscript.php output.

but if you want some additional information be sent to that testscript (like password) you can use curl to send POST data to your script..

just a simplecurl class i googled from hotscripts...


* @author
* @copyright 2007

class curl
var $channel ;

function curl( )
$this->channel = curl_init( );
// you might want the headers for http codes
curl_setopt( $this->channel, CURLOPT_HEADER, true );
// you may need to set the http useragent for curl to operate as
curl_setopt( $this->channel, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
// you wanna follow stuff like meta and location headers
curl_setopt( $this->channel, CURLOPT_FOLLOWLOCATION, true );
// you want all the data back to test it for errors
curl_setopt( $this->channel, CURLOPT_RETURNTRANSFER, true );
// probably unecessary, but cookies may be needed to
curl_setopt( $this->channel, CURLOPT_COOKIEJAR, 'cookie.txt');
// as above
curl_setopt( $this->channel, CURLOPT_COOKIEFILE, 'cookie.txt');
function makeRequest( $method, $url, $vars )
// if the $vars are in an array then turn them into a usable string
if( is_array( $vars ) ):
$vars = implode( '&', $vars );

// setup the url to post / get from / to
curl_setopt( $this->channel, CURLOPT_URL, $url );
// the actual post bit
if ( strtolower( $method ) == 'post' ) :
curl_setopt( $this->channel, CURLOPT_POST, true );
curl_setopt( $this->channel, CURLOPT_POSTFIELDS, $vars );
// return data
return curl_exec( $this->channel );

$curl = new curl( );
// you should not execute both of these, but either way works
echo $curl->makeRequest( 'post', 'http://yourdomain.com/path/to/php.php', 'message=my message goes here' );
echo $curl->makeRequest( 'post', 'http://yourdomain.com/path/to/php.php', array('message=my message goes here', 'type=text') );

09-02-2009, 03:26 PM
Thanks a lot!