Hi,

I have an API script provided to people fetching data from my website. Other than stats information, how can I get to know that which websites are connecting my website's API? Any php function that I can include in my API script to get their domain names then insert into my database?

I am php newbie. It would be appreciated if you can post your complete function codes here.

Thanks,

Richard

Recommended Answers

All 4 Replies

may I know how domains access your API? Is it through GET request or something else?

Example: yourSiteDotCom/api/search?q=something

Hi Veedeoo,

Yes, it's get request as your example. There is no API key needed.

Thanks.

There are PHP predefined variables called $_SERVER. This may help you.

I would look into the following variables

    HTTP_HOST
    HTTP_REFERER

There are plenty of resources online on how to implement this. I just don't have the time today to test it, but if ever you will not be able to find something, please let me know and I will write you the codes if needed.

The second option is making a minor changes on your API specifically on the GET request. So the remote site will have to add piece of codes to their requesting script.. for example, if your API accepts request like this

yourSiteDotCom/api/search?q=something

you can ask them to add something like this

$theirDomain = $_SERVER["HTTP_HOST"]; 

## the new query for sending an API request to your site

yourSiteDotCom/api/search?q=something&rsite=<?php echo $theirDomain;?>

On your API application, you can add simple codes like this

    if(isset($_GET['rsite'])){
    ## this is the domain name of the requesting remote site
    $remotSite = $_GET['rsite'];
    ## we need to clean this up and validate, and make sure it is a valid url
    if(filter_var($remoteSite, FILTER_VALIDATE_URL)){

    ## do what you need to do e.g. like adding it to your database

    }

    }
    ## allow other request from other site to get process.
    ## or you can use the PHP variables I have suggested above.

Nice solution! thank you so much! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.