Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder DR.Wong's Avatar
    Join Date
    Jan 2005
    Posts
    360
    Thanks
    23
    Thanked 1 Time in 1 Post

    Get value of a function on a remote server

    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?
    -DR.Wong

    Wheres the food at?

  • #2
    Regular Coder
    Join Date
    Aug 2009
    Posts
    131
    Thanks
    28
    Thanked 7 Times in 7 Posts
    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?

  • #3
    Regular Coder funnymoney's Avatar
    Join Date
    Aug 2007
    Posts
    364
    Thanks
    17
    Thanked 24 Times in 24 Posts
    Quote Originally Posted by DR.Wong View Post
    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

  • #4
    Regular Coder DR.Wong's Avatar
    Join Date
    Jan 2005
    Posts
    360
    Thanks
    23
    Thanked 1 Time in 1 Post
    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.
    -DR.Wong

    Wheres the food at?

  • #5
    Regular Coder funnymoney's Avatar
    Join Date
    Aug 2007
    Posts
    364
    Thanks
    17
    Thanked 24 Times in 24 Posts
    Quote Originally Posted by DR.Wong View Post
    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

    PHP Code:
    $output file_get_contents("http://www.example.com/testscript.php");
    print 
    $output
    if testscript.php on example.com is

    PHP Code:
    <?php
    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...

    PHP Code:
    <?php

    /**
     * @author 
     * @copyright 2007
     */

    class curl
    {
        var 
    $channel ;
        
        function 
    curl(  )
        {
            
    $this->channel curl_init( );
            
    // you might want the headers for http codes
            
    curl_setopt$this->channelCURLOPT_HEADERtrue );
            
    // you may need to set the http useragent for curl to operate as
            
    curl_setopt$this->channelCURLOPT_USERAGENT$_SERVER['HTTP_USER_AGENT']);
            
    // you wanna follow stuff like meta and location headers
            
    curl_setopt$this->channelCURLOPT_FOLLOWLOCATIONtrue );
            
    // you want all the data back to test it for errors
            
    curl_setopt$this->channelCURLOPT_RETURNTRANSFERtrue );
            
    // probably unecessary, but cookies may be needed to
            
    curl_setopt$this->channelCURLOPT_COOKIEJAR'cookie.txt');
            
    // as above
            
    curl_setopt$this->channelCURLOPT_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 );
            endif;
            
            
    // setup the url to post / get from / to
            
    curl_setopt$this->channelCURLOPT_URL$url );
            
    // the actual post bit
            
    if ( strtolower$method ) == 'post' ) :
                
    curl_setopt$this->channelCURLOPT_POSTtrue );
                
    curl_setopt$this->channelCURLOPT_POSTFIELDS$vars );
            endif;
            
    // 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') );

  • Users who have thanked funnymoney for this post:

    DR.Wong (09-02-2009)

  • #6
    Regular Coder DR.Wong's Avatar
    Join Date
    Jan 2005
    Posts
    360
    Thanks
    23
    Thanked 1 Time in 1 Post
    Thanks a lot!
    -DR.Wong

    Wheres the food at?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •