Sample code for 30+ languages & platforms
PHP Requires Chilkat v11.0.0+

AzureWebsites OAuth2 Password Flow

See more OAuth2 Examples

Demonstrates how to do OAuth 2.0 password flow for azurewebsites.net.

Chilkat PHP Downloads

PHP
<?php

use Chilkat2\Http;
use Chilkat2\HttpRequest;
use Chilkat2\HttpResponse;
use Chilkat2\JsonObject;
use Chilkat2\StringBuilder;

// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

$http = new Http();

$req = new HttpRequest();
$req->HttpVerb = 'POST';
$req->Path = '/token';
$req->ContentType = 'application/x-www-form-urlencoded';
$req->AddParam('grant_type', 'password');
$req->AddParam('username', 'your_username');
$req->AddParam('password', 'your_password');

$tokenEndpoint = 'https://your_api.azurewebsites.net/token';

$resp = new HttpResponse();
try {
    $http->HttpReq($tokenEndpoint, $req, $resp);
} catch (\Chilkat2\Exception $e) {
    echo $e->getMessage(), "\n";
    exit;
}

$sbResponseBody = new StringBuilder();
$resp->GetBodySb($sbResponseBody);
$jResp = new JsonObject();
$jResp->LoadSb($sbResponseBody);
$jResp->EmitCompact = false;

echo "Response Body:\n";
echo $jResp->Emit(), "\n";

// Sample JSON response:

// {
//   "access_token": "NQGHn ... xTS",
//   "token_type": "bearer",
//   "expires_in": 1209599,
//   "userName": "your_username",
//   ".issued": "Mon, 27 Apr 2020 23:49:35 GMT",
//   ".expires": "Mon, 11 May 2020 23:49:35 GMT"
// }

$respStatusCode = $resp->StatusCode;
echo 'Response Status Code = ', $respStatusCode, "\n";
if ($respStatusCode >= 400) {
    echo "Response Header:\n";
    echo $resp->Header, "\n";
    echo "Failed.\n";
    exit;
}

// ----------------------------------
// Use the OAuth2 token in a request.
// For example...

$sbXml = new StringBuilder();
try {
    $sbXml->LoadFile('c:/someDir/someXmlFile.xml', 'utf-8');
} catch (\Chilkat2\Exception $e) {
    echo "Failed to load the XML file.\n";
    exit;
}

// Get the OAuth2 token and use it for authentication
$http->AuthToken = $jResp->StringOf('token');

$destUrl = 'https://your_api.azurewebsites.net/destinationUrl';
try {
    $http->HttpSb('POST', $destUrl, $sbXml, 'utf-8', 'application/xml', $resp);
} catch (\Chilkat2\Exception $e) {
    echo $e->getMessage(), "\n";
    exit;
}

$respStatusCode = $resp->StatusCode;
echo 'Response Status Code = ', $respStatusCode, "\n";
if ($respStatusCode >= 400) {
    echo "Response Header:\n";
    echo $resp->Header, "\n";
    echo "Failed.\n";
    exit;
}

// Examine the response body
echo $resp->BodyStr, "\n";