PHP: Using file_get_contents to access an API – Empty Response

Based on the curl example from the linked API docs, the content type should be application/x-www-form-urlencoded, and so for the initial request to you should be able to follow the example here.

In your case this will look something like:

$url = ''; 

$data = array('grant_type' => 'client_credentials');

$opts = array(
  'http' => array(
    'method' => 'POST',
    'header' => array (
      'Content-type: application/x-www-form-urlencoded',
      'Authorization: Basic ' . base64_encode("$client_id:$client_pass")
    'content' => http_build_query($data)

$context = stream_context_create($opts);

$json = file_get_contents($url, false, $context);

Also, in your code you are storing the raw result from the initial request in $json, then the decoded array in $result, but attempting to get the access_token from the initial request, instead of the decoded array.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top