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

<?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

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 :: echo php dropdown from db and save it in a db 
Php :: laravel model isdirty 
Php :: custom pagination in laravel 
Php :: laravel drop column softdeletes 
Php :: php explode sentence into words 
Php :: search query codeigniter 
Php :: laravel sanctum instalation 
Php :: array_map in php 
Php :: carbon get month from date 
Php :: php into javascript 
Php :: php json_encode remove array index 
Php :: drupal 8 entity_view 
Php :: how to add javascript in php variable 
Php :: php strict mopde 
Php :: laravel migration bigint length 
Php :: php detect daylight saving time DST 
Php :: append variable into string php 
Php :: how to disable screenshot jquery 
Php :: stripe php sdk constants 
Php :: truncate url rewrites magento 2 database 
Php :: php empy a file 
Php :: multiple primary key defined laravel 
Php :: pest check url status 
Php :: get custom field post wordpress dev 
Php :: laravel collection sort by date 
Php :: 1 to many relationship in laravel 
Php :: View [layouts.master] not found 
Php :: php rand between 0 and 1 
Php :: hasmany relation in laravel 
Php :: php convert path from server url to link 
ADD CONTENT
Topic
Content
Source link
Name
9+6 =