Forum Discussion

insoldev's avatar
insoldev
Helper I
8 years ago
Solved

Power BI embedded - switch to premium in php application

Hi there - I'm currently making the switch from Power BI workspace collections to the new model. Previously I used embedded in a PHP application (I'm not a professional PHP developer, though I work with Power BI + BI architecture every day).

 

Unfortunately, all of the documentation is in c# for ASP.net development. 

 

Obviously, I dont wan't to reinvent the wheel, and would need for this to work in PHP which is where our development Minimum Viable Product currently is.

 

This is the code that I used before, which is from an old embedded Microsoft tutorial (which was great!) 

 

Will it be possible to amend the code below for the new model, or do I need to start from scratch?

 

<!--?php 
// 1. power bi access key
$accesskey = "HIDDEN";
// 2. construct input value
$token1 = "{" . "\"typ\":\"JWT\"," . "\"alg\":\"HS256\"" . "}";
$token2 = "{" . "\"wid\":\"HIDDEN\"," .
// workspace id
"\"rid\":\"HIDDEN\"," .
// report id
"\"wcn\":\"HIDDEN\"," .
// workspace collection name "\"iss\":\"HIDDEN\"," . "\"ver\":\"0.2.0\"," . "\"aud\":\"https://analysis.windows.net/powerbi/api\"," . "\"nbf\":" .
date("U") . "," . "\"exp\":" . date("U" , strtotime("+1 hour")) . "}";
$inputval = rfc4648_base64_encode($token1) . "." . rfc4648_base64_encode($token2);
// 3. get encoded signature value
$hash = hash_hmac("sha256", $inputval, $accesskey, true);
$sig = rfc4648_base64_encode($hash);
// 4. get apptoken
$apptoken = $inputval . "." . $sig;
// helper functions
function rfc4648_base64_encode($arg) { $res = $arg;
$res = base64_encode($res);
$res = str_replace("/", "_", $res);
$res = str_replace("+", "-", $res);
$res = rtrim($res, "="); return $res; } ?--> <button id="btnView" class="et_pb_contact_submit et_pb_button">Open Report</button> <div id="divView"><iframe id="ifrTile" width="100%" height="700"></iframe></div> <script> (function () { document.getElementById('btnView').onclick = function() { var iframe = document.getElementById('ifrTile'); iframe.src="https://app.powerbi.com/appTokenReportEmbed?reportId=HIDDEN"; iframe.onload = function() { var msgJson = { action: "loadReport", accessToken: "<?=$apptoken?>", height: 500, width: 722 }; var msgTxt = JSON.stringify(msgJson); iframe.contentWindow.postMessage(msgTxt, "*"); }; }; }()); </script>

  • insoldev wrote:

    Is it effectively the same, though pointing to a app workspace, rather than a workspace collection?


    insoldev

    No, It is pretty different. They use two sets of REST APIs, not to mentions the SDKs. In the new Power BI Embedded based on Azure SKU, it uses the these Power BI REST APIs.

     

    As to your case, I don't know PHP, however I think below demo in HTML and REST API can work regardless of any coding languages.

     

    Simply you can embed report in a HTML like.

    <html>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.js"></script>
    <script src="https://microsoft.github.io/PowerBI-JavaScript/demo/node_modules/powerbi-client/dist/powerbi.js"></script>
    
    <script type="text/javascript">
    window.onload = function () { 
      
     // Read embed application token from Model
        var accessToken = "Embedtoken starts with H4sIxxx"; 
    	
        // Read embed URL from Model
        var embedUrl = "https://app.powerbi.com/reportEmbed?reportId={reportid}&groupId={groupid}";
    
        // Read dashboard Id from Model
        var embedReportId = "{reportid}";
    
        // Get models. models contains enums that can be used.
        var models = window['powerbi-client'].models;	  
    	 
        var config = {
            type: 'report',
            tokenType: models.TokenType.Embed,
            accessToken: accessToken,
            embedUrl: embedUrl,
            id: embedReportId , 
    		pageView: "oneColumn",
    		settings: {
            filterPaneEnabled: true	
        }		 
        };
        // Get a reference to the embedded dashboard HTML element
        var dashboardContainer = $('#reportContainer')[0] ;
        // Embed the dashboard and display it within the div container.
    var report = powerbi.embed(dashboardContainer, config);   
    } 
     
    </script> 
     
    <div id="reportContainer"></div>
    
    </html>  

    You can get the reportid from the forementioned REST APIs. As to embed token, you can reference my reply in this thread.

4 Replies

  • Is it effectively the same, though pointing to a app workspace, rather than a workspace collection?

    • Eric_Zhang's avatar
      Eric_Zhang
      Microsoft Employee

      insoldev wrote:

      Is it effectively the same, though pointing to a app workspace, rather than a workspace collection?


      insoldev

      No, It is pretty different. They use two sets of REST APIs, not to mentions the SDKs. In the new Power BI Embedded based on Azure SKU, it uses the these Power BI REST APIs.

       

      As to your case, I don't know PHP, however I think below demo in HTML and REST API can work regardless of any coding languages.

       

      Simply you can embed report in a HTML like.

      <html>
      <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.js"></script>
      <script src="https://microsoft.github.io/PowerBI-JavaScript/demo/node_modules/powerbi-client/dist/powerbi.js"></script>
      
      <script type="text/javascript">
      window.onload = function () { 
        
       // Read embed application token from Model
          var accessToken = "Embedtoken starts with H4sIxxx"; 
      	
          // Read embed URL from Model
          var embedUrl = "https://app.powerbi.com/reportEmbed?reportId={reportid}&groupId={groupid}";
      
          // Read dashboard Id from Model
          var embedReportId = "{reportid}";
      
          // Get models. models contains enums that can be used.
          var models = window['powerbi-client'].models;	  
      	 
          var config = {
              type: 'report',
              tokenType: models.TokenType.Embed,
              accessToken: accessToken,
              embedUrl: embedUrl,
              id: embedReportId , 
      		pageView: "oneColumn",
      		settings: {
              filterPaneEnabled: true	
          }		 
          };
          // Get a reference to the embedded dashboard HTML element
          var dashboardContainer = $('#reportContainer')[0] ;
          // Embed the dashboard and display it within the div container.
      var report = powerbi.embed(dashboardContainer, config);   
      } 
       
      </script> 
       
      <div id="reportContainer"></div>
      
      </html>  

      You can get the reportid from the forementioned REST APIs. As to embed token, you can reference my reply in this thread.

      • insoldev's avatar
        insoldev
        Helper I

        Thanks Eric - I don't have time to verify, though that is really helpful.