Giter Club home page Giter Club logo

simple_php_api's Introduction

Simple PHP API

This script allows other sites to post data to us and then we can use that data to save in database or any other purpose. The code of API itself is in index.php file.

We give them fixed secret key and they must pass that secret via bearer token to be able to call our API.

This example will actually log posted data into log.txt file.

Usage

// Set the request URL and HTTP method
$url = 'https://domain.com/api/';

// Set the request body (if applicable)
$data = array('field1' => 'value1', 'field2' => 'value2');

$options = array(
    CURLOPT_URL => $url,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_POSTFIELDS => http_build_query($data),
    CURLOPT_HTTPHEADER => array(
        'Content-Type: application/x-www-form-urlencoded',
        'Authorization: Bearer 0cd5cb10880d70d7c45eb53c07a2757bf38e933b456fc1594e3ca9a3dd40d9e5'
    ),
);

// Send the request
$curl = curl_init();
curl_setopt_array($curl, $options);
$result = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);

// Handle the response
if ($status !== 200) {
    // Request failed
    echo "Request failed with status code $status\n";
} else {
    // Request succeeded
    $response = json_decode($result);
    print_r($response);
}

Note that in above example, we have used PHP to call our API but other sites can call our API in any language, they just need to send POST request to us and also add Authorization header in their request with bearer secret that we will provide them.


NOTE: If Authorization is not being passed correctly to our API, we can use put .htaccess file from this code in same folder where this API will reside.

simple_php_api's People

Contributors

sarfraznawaz2005 avatar sarfrazonsupport avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.