Power BI Embedded example using cURL and PHP

Recently Microsoft re-launched PowerBI Embedded as part of the PowerBI Premium offering. Instead of a pay-per-view pricing model Microsoft now offers dedicated capacity. There are three parts to get this new PowerBI Embedded working:

  1. Configuration in Azure (Register an app, activate the PowerBI Embedded capacity)
  2. Configuration in the PowerBI service (create a PowerBI app and activate the premium capacity)
  3. Development of a web application (requesting a token, requesting the embedded url and embed the actual report using javascript)

All above is explained in detail in the getting started guide. However, the last part is explained using a .NET example app. In this post we’ll show an alternative method using PHP and cURL.

Our basic PHP example uses two cURL requests: the first request will request a token, the second request uses this token to retrieve the embedded URL of the report. We’ll store the token and the embedded URL in PHP variables and use these in the JavaScript code to finally embed the report in an HTML div attribute.

Steps:

  1. Request the token
  2. Decode the result and store the token in a variable
  3. Request the embedded URL using the token variable
  4. Store the embedded URL in a variable
  5. Embed the report with Javascript using the two variables above

 

Step 1) Request the token:

/* Get oauth2 token using a POST request */

$curlPostToken = curl_init();

curl_setopt_array($curlPostToken, array(

CURLOPT_URL => "https://login.windows.net/common/oauth2/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 => array(

grant_type => 'password',

scope => 'openid',

resource => 'https://analysis.windows.net/powerbi/api',

client_id => '', // Registered App ApplicationID

username => '', // for example john.doe@yourdomain.com

password => '' // Azure password for above user

)

));

$tokenResponse = curl_exec($curlPostToken);

$tokenError = curl_error($curlPostToken);

curl_close($curlPostToken);

 

Step 2) Decode the result and store the token in a variable:

// decode result, and store the access_token in $embeddedToken variable:

$tokenResult = json_decode($tokenResponse, true);

$token = $tokenResult["access_token"];

$embeddedToken = "Bearer "  . ' ' .  $token;

 

Step 3) Use this token variable in a GET request to get the embedded url:

/*      Use the token to get an embedded URL using a GET request */

$curlGetUrl = curl_init();

 

curl_setopt_array($curlGetUrl, array(

CURLOPT_URL => "https://api.powerbi.com/v1.0/myorg/groups/YourGroupID/reports/",

CURLOPT_RETURNTRANSFER => true,

CURLOPT_ENCODING => "",

CURLOPT_MAXREDIRS => 10,

CURLOPT_TIMEOUT => 30,

CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,

CURLOPT_CUSTOMREQUEST => "GET",

CURLOPT_HTTPHEADER => array(

"Authorization: $embeddedToken",

"Cache-Control: no-cache",

),

));

$embedResponse = curl_exec($curlGetUrl);

$embedError = curl_error($curlGetUrl);

curl_close($$curlGetUrl);

 

Step 4) Decode the result, retrieve the embedded URL and store this URL in a second variable:

if ($embedError) {

echo "cURL Error #:" . $embedError;

} else {

$embedResponse = json_decode($embedResponse, true);

$embedUrl = $embedResponse['value'][0]['embedUrl'];

}

 

5. In the last part we use the JavaScript code provided by Microsoft to embed the report:

<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>

<script src="scripts/powerbi.js"></script>

<div id="reportContainer"></div>

 

<script>

// Get models. models contains enums that can be used.

var models = window['powerbi-client'].models;

// Embed configuration used to describe the what and how to embed.

// This object is used when calling powerbi.embed.

// This also includes settings and options such as filters.

// You can find more information at https://github.com/Microsoft/PowerBI-JavaScript/wiki/Embed-Configuration-Details.

var embedConfiguration= {

type: 'report',

id: 'YourReportId', // the report ID

embedUrl: "<?php echo $embedUrl ?>",

accessToken: "<?php echo $token; ?>" ,

};

var $reportContainer = $('#reportContainer');

var report = powerbi.embed($reportContainer.get(0), embedConfiguration);

</script>

Lars Bouwens is Business Intelligence consultant at Motion10 and likes to read and write about Power BI and Azure.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

9 Comments

  1. Thanks for the tutorial, it helped a lot.
    I have a question, when displaying the report appears the field: ... Export data, does it know in any way to remove this?

    thank you

  2. Stan Evers

    Nice blog Lars! Great to see you found an alternative to embed Power BI with PHP and cURL.

  3. a debt of gratitude is in order for sharing data exceptionally pleasant blog thank you lars!

  4. Howard

    Thanks Lars. Would you have any idea where to start if I constantly get wrong username/password when requesting the token in step 1? I've confirmed my details are good, tried it every possible way, but still get the error. If there is any place to even start troubleshooting this, it would be great! If not, no problem, thanks for helping by posting the code!

  5. udhayavanan

    thanks for the tutorial which made easy,
    but i forget the missing option publish to web. without publish to web how i can get embed url. is there any option?

    always i am getting this content is not available message.

Next ArticleImplementing ‘Always Encrypted’ in an ETL process