Skip to content

Instantly share code, notes, and snippets.

@itayher
Last active October 18, 2016 20:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save itayher/daaa357343e8162bb38727c5f0027233 to your computer and use it in GitHub Desktop.
Save itayher/daaa357343e8162bb38727c5f0027233 to your computer and use it in GitHub Desktop.

Get Access Token

HttpRequest:

<?php

$request = new HttpRequest();
$request->setUrl('https://api.backand.com/token');
$request->setMethod(HTTP_METH_POST);

$request->setHeaders(array(
  'cache-control' => 'no-cache',
  'content-type' => 'application/x-www-form-urlencoded',
  'accept' => 'Accept:application/json'
));

$request->setContentType('application/x-www-form-urlencoded');
$request->setPostFields(array(
  'username' => ‘user@mellanox.com',
  'password' => ‘XXX',
  'grant_type' => 'password',
  'appName' => 'mellanoxstart'
));

try {
  $response = $request->send();

  echo $response->getBody();
} catch (HttpException $ex) {
  echo $ex;
}

PHP cURL

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://api.backand.com/token",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "username=user%40mellanox.com&password=XXX&grant_type=password&appName=mellanoxstart",
  CURLOPT_HTTPHEADER => array(
    "accept: Accept:application/json",
    "cache-control: no-cache",
    "content-type: application/x-www-form-urlencoded"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment