?
Solved

PHP: How to call a function with Parameters and accept return value from another URL/Website

Posted on 2016-12-01
6
Medium Priority
?
86 Views
Last Modified: 2016-12-02
I have a two websites both running my PHP modules.
Website-1 requires to make a call to a particular PHP function on website-2 with few parameters.
How do I do this in the simplest manner.

Website-1 uses
PHP Version 5.2.17
0
Comment
Question by:Allan_Fernandes
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 41908435
You can only 'call' a PHP file on the second host.  There are two methods, GET and POST.  GET is a normal link like you see in your address bar.  It can have the parameters in the query string that is at the end of it.  POST is usually generated by an HTML form.  The parameters are in the form fields.

How are you 'calling' the PHP page on the second site?  You would normally have to duplicate that on the first site for it to work properly.
0
 
LVL 59

Expert Comment

by:Julian Hansen
ID: 41908483
Is this

- Server to Server
- Client to server

Option 1
Your PHP script on Server1 needs to call a PHP service on another Server. The exchange is done between the servers without involving the client (Browser)

Option 2
The HTML page POSTS to the server either using a standard Form post or by means of an AJAX (CORS) request

Which one you use depends on the following

- Where do you need the data
- Do you need to stay on the same page or will you transition between pages
- When you "call" to server 2 - is this to get data or to interact with the server as if you had browsed to it.

There is not enough information in your question to give a definitive answer - can you elaborate.
0
 

Author Comment

by:Allan_Fernandes
ID: 41908489
I had checked the syntax of _Get and what it seems like it can call only within that PHP module.
like I am already using

echo $_GET["job"]($_GET["args"]);

But if I want to use it thus it is not working

echo $_GET["http://nnn.nnn.nnn/Regpath/Register.php?job=ChkOnlineRegistration&args=".$wRecvText]

I cannot duplicate the PHP module on WebSite-1 because it is being used by my Web Development team.
Whereas Website-2 is a VPS handled only by me. It consists of my critical code.
I want the liberty to move Website-2 as and when I want therefore Website-1 is the stable front for my application's communications.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:Allan_Fernandes
ID: 41908496
Just saw your message Hansen. I will elaborate little further. I have a desktop application built in Delphi (not browser based).
I am in the process of giving my users certain online facilities. From Delphi I simply send the required URL in a httpclient and
I get the results back.
0
 
LVL 59

Accepted Solution

by:
Julian Hansen earned 2000 total points
ID: 41908570
echo $_GET["job"]($_GET["args"]);

Open in new window


The above scares me - you might want to hang a big red sign on your server that says "HACK ME"

What you are doing here is dynamically calling a function based on parameters from a URL

What you want to do rather is something like this

// Make sure you got a job and assign if not
$job = isset($_GET['job']) ? $_GET['job'] : false;

// Now check if there was something in it
if ($job) {
   // obfuscate the name so you can't have people sending arbitary function names in
   $action = "__{$job}";
   
   // make sure there is such a function
   if (function_exists($action)) {
       $args = isset($_GET['args']) ? $_GET['args'] : null;

       // safely call your function
       $action($args);
   }
}

// call with ?job=doSomethingUseful
function __doSomethingUseful()
{
}

Open in new window


That sorts that out.

Now you want to call the function on another server .

To do this we need to use cUrl (http://php.net/manual/en/book.curl.php)
Sample from here http://php.net/manual/en/curl.examples.php
// Because you are using GET we only need the URL
function callRemoteFunction($url)
{
    // create curl resource
        $ch = curl_init();

        // set url
        curl_setopt($ch, CURLOPT_URL, $url);

        //return the transfer as a string
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        // $output contains the output string
        $output = curl_exec($ch);

        // close curl resource to free up system resources
        curl_close($ch);

        return $output;
}
define('URL','http://nnn.nnn.nnn/Regpath/Register.php');
$job = 'ChkOnlineRegistration';
$args = $wRecvText;

$path = URL . "?job={$job}&args={$args}";
$result = callRemoteFunction($path);
// do something with result

Open in new window

Sample available if needed
1
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 41908578
What you want is called an Application Programming Interface ("API").  This article discusses the issues and gives code examples.
https://www.experts-exchange.com/articles/12239/Introduction-to-Application-Programming-Interfaces.html

PHP 5.2 is dangerously out of date, and no longer supported, not even for security issues.  Current is PHP7+, or any version listed under "Download" on the home page of PHP.net.  Time to upgrade!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question