Search
 
SCRIPT & CODE EXAMPLE
 

PHP

call api with php

// Method: POST, PUT, GET etc
// Data: array("param" => "value") ==> index.php?param=value

function CallAPI($method, $url, $data = false)
{
    $curl = curl_init();

    switch ($method)
    {
        case "POST":
            curl_setopt($curl, CURLOPT_POST, 1);

            if ($data)
                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
            break;
        case "PUT":
            curl_setopt($curl, CURLOPT_PUT, 1);
            break;
        default:
            if ($data)
                $url = sprintf("%s?%s", $url, http_build_query($data));
    }

    // Optional Authentication:
    curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($curl, CURLOPT_USERPWD, "username:password");

    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

    $result = curl_exec($curl);

    curl_close($curl);

    return $result;
}
Comment

php api connection

<?php
header('Access-Control-Allow-Origin: *');

header('Content-Type: application/json; charset=UTF-8');

header("Access-Control-Allow-Methods: GET");

header("Access-Control-Max-Age:3600");

header("Access-Control-Allow-Headers:*");
Comment

rest api php

<?php
$con = mysqli_connect("Localhost", "root", "", "rest_api");
if (mysqli_connect_errno())
{
    echo "Connection Fail" . mysqli_connect_error();
}
header('Content-Type:application/json');
if (isset($_GET['token']))
{
    $token = mysqli_real_escape_string($con, $_GET['token']);
    $checkTokenRes = mysqli_query($con, "select * from api_token where token='$token'");
    if (mysqli_num_rows($checkTokenRes) > 0)
    {
        $checkTokenRow = mysqli_fetch_assoc($checkTokenRes);
        if ($checkTokenRow['status'] == 1)
        {
            if ($checkTokenRow['hit_limit'] <= $checkTokenRow['hit_count'])
            {
                $status = 'true';
                $data = "Api hit limit exceed";
                $code = '6';
            }
            else
            {
                mysqli_query($con, "UPDATE `api_token` SET `hit_count`= hit_count+1 WHERE `token` = '$token'");
                $sql = "select * from collected_data ";
                if (isset($_GET['id']) && $_GET['id'] > 0)
                {
                    $id = mysqli_real_escape_string($con, $_GET['id']);
                    $sql .= " where id='$id'";
                }
                $sqlRes = mysqli_query($con, $sql);
                if (mysqli_num_rows($sqlRes) > 0)
                {
                    $data = [];
                    while ($row = mysqli_fetch_assoc($sqlRes))
                    {
                        $data[] = $row;
                    }
                    $status = 'true';
                    $code = '5';
                }
                else
                {
                    $status = 'true';
                    $data = "Data not found";
                    $code = '4';
                }

            }

        }
        else
        {
            $status = 'true';
            $data = "API token deactivated";
            $code = '3';
        }
    }
    else
    {
        $status = 'true';
        $data = "Please provide valid API token";
        $code = '2';
    }

}
else
{
    $status = 'true';
    $data = "Please provide API token";
    $code = '1';
}
echo json_encode(["status" => $status, "data" => $data, "code" => $code]);
?>
Comment

php get api

// simple steps

// Method: POST, PUT, GET etc
// Data: array("param" => "value") ==> index.php?param=value


function CallAPI($method, $url, $data = false)
{
    $curl = curl_init();

    switch ($method)
    {
        case "POST":
            curl_setopt($curl, CURLOPT_POST, 1);

            if ($data)
                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
            break;
        case "PUT":
            curl_setopt($curl, CURLOPT_PUT, 1);
            break;
        default:
            if ($data)
                $url = sprintf("%s?%s", $url, http_build_query($data));
    }

    // Optional Authentication:
    curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($curl, CURLOPT_USERPWD, "username:password");

    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

    $result = curl_exec($curl);

    curl_close($curl);

    return $result;
}

Comment

external api php

<?php
function getServerStatistics($url) {
    $statisticsJson = file_get_contents($url);
    if ($statisticsJson === false) {
       return false;
    }

    $statisticsObj = json_decode($statisticsJson);
    if ($statisticsObj !== null) {
       return false;
    }

    return $statisticsObj;
}

// ...

$stats = getServerStatistics($url);
if ($stats !== false) {
    print $stats->players->online;
}
Comment

PREVIOUS NEXT
Code Example
Php :: php unit expect exception 
Php :: PHP rtrim — Strip whitespace (or other characters) from the end of a string 
Php :: laravel simple pagination 
Php :: php lcfirst 
Php :: php sort by key 
Php :: reverse string php 
Php :: call jquery function in php code 
Php :: unique check two clolumn in laravel validation 
Php :: woocommerce set default shipping country 
Php :: how to extract data from json in php 
Php :: referencing constant in config laravel 
Php :: codeigniter select where 
Php :: auto reload for laravel 
Php :: js php number format space 
Php :: how to rename a table element in laravel 
Php :: fallo al conectar al servidor ftp wordpress 
Php :: convert to string php 
Php :: laravel call controller method from another controller 
Php :: spatie laravel pdf image 
Php :: storefront remove sidebar from product page 
Php :: php audio embed 
Php :: Laravel 8 Auth Scaffolding using Inertia Jetstream 
Php :: get 2 hrs before data in php 
Php :: check mobile number length in php 
Php :: php version not update after windows env file 
Php :: php declare variable 
Php :: magento 2 remove order 
Php :: laravel eloquent with nested 
Php :: 0 
Php :: php heredoc function 
ADD CONTENT
Topic
Content
Source link
Name
5+2 =