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

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

how to return an api response in phpo

header('Content-Type: application/json; charset=utf-8');
echo json_encode($response); // the response is sent back to the browser
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 :: microft access request database with pdo 
Php :: Remove .php extension & Remove trailing slash 
Php :: laravel 8 with jetstream 
Php :: Getting the closest string match using php 
Php :: PHP Fatal error: Allowed memory size of 1610612736 bytes exhausted 
Php :: laravel collection collapse 
Php :: merge array in php 
Php :: magento2 move Exception #0 (Exception): Notice: Undefined offset: 2 in /var/www/nucleus/htdocs/vendor/magento/framework/Encryption/Encryptor.php on line 591 
Php :: laravel date diff 
Php :: array_merge in php 
Php :: php conditionals 
Php :: php key_exists 
Php :: wordpress args 
Php :: laravel create many to many table 
Php :: php extract number from string without comma 
Php :: create auto image path folder in laravel 8 
Php :: executar comando linux php 
Php :: php aes 
Php :: convert collection to array laravel 
Php :: php base convert 
Php :: laravel generate unique string 
Php :: Laravel route not calling function of controller 
Php :: encryption and decryption in php example 
Php :: laravel pagination 
Php :: PDO Prepared Statement php 
Php :: php tutorial 
Php :: add character after x characters in php 
Php :: php get array key like 
Php :: is legged in wodpress 
Php :: yii2 sendemail extension 
ADD CONTENT
Topic
Content
Source link
Name
8+3 =