Skip to content
Advertisement

PHP – Tunneling the API

I don’t know what this method called : Reverse Proxy , Tunneling, Bypassing ?

Imagine, we have 3 website :

JavaScript

1. I bought the API or Payment Gateway service for example1.com , the website don’t have any content , forms, data. (Just to get the API)

2. Now, I want create tunnel between example2.com | example1.com | exampleapi.com. I mean, get all request from example2.com , send to example1.com and pass the data to exampleapi.com.

3. And after response, the exampleapi.com.com send data to example1.com and example2.com will receive it and showing to the user.

Advertisement

Answer

Here’s an example of how you can write your code. You can simulate this on a single site like I did. On my site, I created 3 folders:

  • example1
  • example2
  • exampleapi

Under each of them, I create an index.php file. Let’s look at them.

yoursite.com/exampleapi/index.php

JavaScript

The output of this is plaintext

JavaScript

We’ll call this API from example1.

yoursite.com/example1/index.php

This site’s code will have it’s own data and will pull data from exampleapi/index.php like so:

JavaScript

Output of this code will be

JavaScript

We’ll call this from example2.

yoursite.com/example2/index.php

This will be your webpage. I have simulated a call. When the button is pressed, PHP will send a request to example1/index.php. And that page will send a request to exampleapi/index.php, fetch the data, combine the fetched data with its own data and send it back to example2/index.php

JavaScript

When you go to yoursite.com/example2/index.php, you’ll see something like this:

enter image description here

When you press the button, example2/index.php -> calls example1/index.php -> calls exampleapi/index.php. Data goes in reverse and you’ll see this kind of an output:

enter image description here

This shows you how you can use PHP one 1 page to call API on another page. If you are in control of that another page, you can tweak code to call API from yet-another-page.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement