{"id":293,"date":"2024-11-14T08:09:31","date_gmt":"2024-11-14T02:39:31","guid":{"rendered":"https:\/\/griffso.com\/blogs\/?p=293"},"modified":"2024-11-14T08:09:41","modified_gmt":"2024-11-14T02:39:41","slug":"power-bi-ci","status":"publish","type":"post","link":"https:\/\/griffso.com\/blogs\/power-bi-ci\/","title":{"rendered":"Integrating Power BI with CodeIgniter: A Step-by-Step Guide"},"content":{"rendered":"\n<p>To integrate Power BI with a PHP CodeIgniter application, I&#8217;ll guide you through the steps and how you might share it effectively on your WordPress blog. This guide will cover authentication, embedding reports, and handling data securely.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Step 1: Prerequisites<\/h1>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Power BI Account<\/strong>: Ensure you have a Power BI Pro or Premium account with access to the workspace containing the reports.<\/li>\n\n\n\n<li><strong>Azure AD Application<\/strong>: Register an application in Azure Active Directory (AD) to authenticate your application and access Power BI reports.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Registering an Application in Azure AD<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Go to the <a href=\"https:\/\/portal.azure.com\/\" target=\"_blank\" rel=\"noopener\">Azure portal<\/a>, navigate to <strong>Azure Active Directory<\/strong> > <strong>App registrations<\/strong>.<\/li>\n\n\n\n<li>Click <strong>New registration<\/strong> and provide:<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Name<\/strong>: Choose a name for your app.<\/li>\n\n\n\n<li><strong>Redirect URI<\/strong>: Set it to your callback URL, for example, <code>https:\/\/your-codeigniter-app.com\/auth\/redirect<\/code>.<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Once created, copy the <strong>Application (client) ID<\/strong> and <strong>Directory (tenant) ID<\/strong>.<\/li>\n\n\n\n<li>Navigate to <strong>Certificates &amp; Secrets<\/strong> and create a new client secret. Copy this as well.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Setting Up Power BI Permissions<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>In the <strong>API permissions<\/strong> section, add <strong>Power BI Service<\/strong> > <strong>Delegated permissions<\/strong>.<\/li>\n\n\n\n<li>Select permissions like <code>Report.Read.All<\/code> or <code>Dataset.ReadWrite.All<\/code> depending on what access you need.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Step 4: Integrating Power BI in CodeIgniter<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Install GuzzleHTTP<\/strong>: Install <a href=\"https:\/\/docs.guzzlephp.org\/\" target=\"_blank\" rel=\"noopener\">Guzzle<\/a> to handle HTTP requests.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   composer require guzzlehttp\/guzzle<\/code><\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>Controller Setup<\/strong>: Create a new controller, <code>PowerbiController.php<\/code>, in CodeIgniter to handle the authentication and report embedding.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Step 5: Writing the Authentication Logic<\/h3>\n\n\n\n<p>In <code>PowerbiController.php<\/code>, add the following functions:<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">1. <code>authenticate()<\/code>: Fetch the access token using client credentials.<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>use GuzzleHttp\\Client;\n\nclass PowerbiController extends CI_Controller {\n    private $clientId = 'YOUR_CLIENT_ID';\n    private $clientSecret = 'YOUR_CLIENT_SECRET';\n    private $tenantId = 'YOUR_TENANT_ID';\n    private $authorityUrl = 'https:\/\/login.microsoftonline.com\/';\n    private $scope = 'https:\/\/analysis.windows.net\/powerbi\/api\/.default';\n\n    public function authenticate() {\n        $client = new Client();\n        $response = $client-&gt;post($this-&gt;authorityUrl . $this-&gt;tenantId . '\/oauth2\/v2.0\/token', &#91;\n            'form_params' =&gt; &#91;\n                'client_id' =&gt; $this-&gt;clientId,\n                'client_secret' =&gt; $this-&gt;clientSecret,\n                'scope' =&gt; $this-&gt;scope,\n                'grant_type' =&gt; 'client_credentials',\n            ]\n        ]);\n\n        $token = json_decode($response-&gt;getBody())-&gt;access_token;\n        return $token;\n    }\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">2. <code>embedReport($reportId, $groupId)<\/code>: Use the token to get an embeddable report.<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>public function embedReport($reportId, $groupId) {\n    $token = $this-&gt;authenticate();\n\n    $client = new Client();\n    $response = $client-&gt;get(\"https:\/\/api.powerbi.com\/v1.0\/myorg\/groups\/{$groupId}\/reports\/{$reportId}\", &#91;\n        'headers' =&gt; &#91;\n            'Authorization' =&gt; 'Bearer ' . $token\n        ]\n    ]);\n\n    $reportDetails = json_decode($response-&gt;getBody());\n    $embedUrl = $reportDetails-&gt;embedUrl;\n\n    \/\/ Pass embedUrl to view\n    $data&#91;'embedUrl'] = $embedUrl;\n    $data&#91;'accessToken'] = $token;\n\n    $this-&gt;load-&gt;view('powerbi_embed_view', $data);\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 6: Embedding the Power BI Report in the View<\/h3>\n\n\n\n<p>Create <code>powerbi_embed_view.php<\/code> in your views folder to display the Power BI report.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n&lt;head&gt;\n    &lt;title&gt;Power BI Report&lt;\/title&gt;\n    &lt;script src=\"https:\/\/cdn.powerbi.com\/libs\/powerbi-client\/2.19.0\/powerbi.min.js\"&gt;&lt;\/script&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n    &lt;div id=\"reportContainer\" style=\"height:600px; width:100%;\"&gt;&lt;\/div&gt;\n\n    &lt;script&gt;\n        var models = window&#91;'powerbi-client'].models;\n        var embedConfig = {\n            type: 'report',\n            tokenType: models.TokenType.Embed,\n            accessToken: '&lt;?= $accessToken ?&gt;',\n            embedUrl: '&lt;?= $embedUrl ?&gt;',\n            settings: {\n                filterPaneEnabled: false,\n                navContentPaneEnabled: true\n            }\n        };\n\n        var reportContainer = document.getElementById('reportContainer');\n        powerbi.embed(reportContainer, embedConfig);\n    &lt;\/script&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 7: Adding Security Measures<\/h3>\n\n\n\n<p>For production environments, ensure:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Tokens are stored securely and refreshed regularly.<\/li>\n\n\n\n<li>User permissions are validated before allowing access.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Posting on Your WordPress Blog<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Introduction<\/strong>: Explain why Power BI integration is valuable for web applications.<\/li>\n\n\n\n<li><strong>Setup Steps<\/strong>: Use the steps provided here, breaking down each section for easy readability.<\/li>\n\n\n\n<li><strong>Code Samples<\/strong>: Embed code snippets using a plugin like <em>SyntaxHighlighter Evolved<\/em>.<\/li>\n\n\n\n<li><strong>Visuals<\/strong>: Add screenshots or a demo video to show the integration in action.<\/li>\n\n\n\n<li><strong>Common Issues and Solutions<\/strong>: List possible errors like authentication failures or permission errors with solutions.<\/li>\n<\/ol>\n<div class=\"pld-like-dislike-wrap pld-template-1\">\n    <div class=\"pld-like-wrap  pld-common-wrap\">\n    <a href=\"javascript:void(0)\" class=\"pld-like-trigger pld-like-dislike-trigger  \" title=\"\" data-post-id=\"293\" data-trigger-type=\"like\" data-restriction=\"cookie\" data-already-liked=\"0\">\n                        <i class=\"fas fa-thumbs-up\"><\/i>\n                <\/a>\n    <span class=\"pld-like-count-wrap pld-count-wrap\">1    <\/span>\n<\/div><div class=\"pld-dislike-wrap  pld-common-wrap\">\n    <a href=\"javascript:void(0)\" class=\"pld-dislike-trigger pld-like-dislike-trigger  \" title=\"\" data-post-id=\"293\" data-trigger-type=\"dislike\" data-restriction=\"cookie\" data-already-liked=\"0\">\n                        <i class=\"fas fa-thumbs-down\"><\/i>\n                <\/a>\n    <span class=\"pld-dislike-count-wrap pld-count-wrap\"><\/span>\n<\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>To integrate Power BI with a PHP CodeIgniter application, I&#8217;ll guide you through the steps and how you might share it effectively on your WordPress blog. This guide will cover authentication, embedding reports, and handling data securely. Step 1: Prerequisites Step 2: Registering an Application in Azure AD Step 3: Setting Up Power BI Permissions [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-container-style":"default","site-container-layout":"default","site-sidebar-layout":"default","disable-article-header":"default","disable-site-header":"default","disable-site-footer":"default","disable-content-area-spacing":"default","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4,3],"tags":[361],"class_list":["post-293","post","type-post","status-publish","format-standard","hentry","category-coding-help-programming-tips-code-examples-coding-resources-debug-guide-software-development-learn-to-code-coding-best-practices-coding-challenges-coding-tutorials","category-here-are-slug-suggestions-for-a-career-category-comma-separated-career-guidance-career-development-career-opportunities-career-advice-career-paths-job-search-tips-career-resources-profes","tag-php-2"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/griffso.com\/blogs\/wp-json\/wp\/v2\/posts\/293","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/griffso.com\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/griffso.com\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/griffso.com\/blogs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/griffso.com\/blogs\/wp-json\/wp\/v2\/comments?post=293"}],"version-history":[{"count":1,"href":"https:\/\/griffso.com\/blogs\/wp-json\/wp\/v2\/posts\/293\/revisions"}],"predecessor-version":[{"id":294,"href":"https:\/\/griffso.com\/blogs\/wp-json\/wp\/v2\/posts\/293\/revisions\/294"}],"wp:attachment":[{"href":"https:\/\/griffso.com\/blogs\/wp-json\/wp\/v2\/media?parent=293"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/griffso.com\/blogs\/wp-json\/wp\/v2\/categories?post=293"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/griffso.com\/blogs\/wp-json\/wp\/v2\/tags?post=293"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}