Getting Started with SQL Server Reporting Services (SSRS) and PHP

At the Intersection of PHP and Microsoft

Getting Started with SQL Server Reporting Services (SSRS) and PHP

  • Comments 48

In a recent post I took a look at how to get started with SQL Server Reporting Services (SSRS). In this post I’ll dive into the SSRS SDK for PHP that was recently released by the Interoperability team here at Microsoft. The SSRS SDK for PHP allows you to render render reports from within your PHP code. The documentation in the SDK is complete with explanations for the classes that make up the SDK, code examples, and a “hello world” example. However, in this post, I’ll build a simple web page (code attached to this post) that renders the Sales report that I created last week. My example uses some reporting features that aren’t shown in the “hello world” example, and this way you’ve got multiple examples to work from when you want to use the SDK.

[Edit: If you have trouble setting up user permissions with SSRS, this video may be helpful: http://www.youtube.com/watch?v=PSYHqkhhdOo.)

Overview

With the Business Intelligence Development Studio you can design reports in SQL Server 2008 Reporting Services. You can access these reports directly via the Report Server (as I showed in this post) or you can use the SSRS SDK for PHP, which uses the SOAP API to communicate with the SQL Server Reporting Services Web Service.

image

Prerequisites

Follow the steps in this post for creating the Sales report (based on the Northwind database): http://blogs.msdn.com/brian_swan/archive/2010/04/29/getting-started-with-sql-server-reporting-services-ssrs.aspx.

Create a User with the Right Permissions

I struggled a bit to create a new User that has the permissions necessary to access reports. Perseverance paid off, however. Here’s what I did:

  1. Create a local Windows user (PHPDemoUser) on your machine. You do not need to make PHPDemoUser an Administrator...a Standard user is fine.
  2. Create a login on your SQL Server Express installation for PHPDemoUser. When you create this login you'll want to qualify it with your machine name: MACHINE_NAME\PHPDemoUser.
  3. Add PHPDemoUser as a user for the Northwind and ReportServer$SQLEXPRESS databases. Give the user dbdatareader and dbdatawriter priviliges.
  4. Modify the rsreportserver.config configuration file located under the ReportServer installation folder (C:\Program Files\Microsoft SQL Server \MSRS10.SQLEXPRESS\Reporting Services\ReportServer in most common scenarios). You might need to open the text editor with Administrator privileges. Add the <RSWindowsBasic/> element to the <AuthenticationTypes> node (if other nodes are already included, don't delete them):

<Authentication>
    <AuthenticationTypes>
        <RSWindowsNegotiate/>
        <RSWindowsNTLM/>
        <RSWindowsBasic/>
    </AuthenticationTypes>
    <EnableAuthPersistence>true</EnableAuthPersistence>
</Authentication>

Connect to the Report Server Web Service

To connect to the Report Server Web Service, we simply need to create a new SSRSReport object (passing in the credentials of the PHPDemoUser and the service URL). If we are toggling an item in the report (as we would do when drilling into the products that make up a category in the Sales report), we call the ToggleItem method. Otherwise, we simply load the report:

require_once 'SSRSReport.php';
define("UID", 'MACHINE_NAME\PHPDemoUser');
define("PWD", "PHPDemoUser_PWD");
define("SERVICE_URL", "
http://localhost/ReportServer_SQLEXPRESS/");
define("REPORT", "/Sales");

$ssrs_report = new SSRSReport(new Credentials(UID, PWD), SERVICE_URL);
if (isset($_REQUEST['rs:ShowHideToggle']))
{
    $ssrs_report->ToggleItem($_REQUEST['rs:ShowHideToggle']);
}
else
{
    $ssrs_report->LoadReport2(REPORT, NULL);
}

Depending on how a report is designed, methods other than ToggleItem exist for rendering a report. For example, if your report allows a user to sort items, the Sort2 method is available. (For a complete list of methods on the SSRSReport class, see the SSRS SDK for PHP User Guide.htm file in the SDK.)

Note: In Reporting Services 2008, new functionality was introduced that required altering some methods. To maintain backwards compatibility with older Reporting Services releases, new methods were created (with the same name as their “old” counterparts) and post-fixed with “2”.

Render the Report as HTML

Next (and last), we render the report as HTML by creating a new RenderAsHTML object. (Other methods allow you to render a report as Excel, Word, CSV or Text, XML, Image, and PDF.)

$renderAsHTML = new RenderAsHTML();
$renderAsHTML->ReplacementRoot = getPageURL();
$result_html = $ssrs_report->Render2($renderAsHTML,
                                     PageCountModeEnum::$Estimate,
                                     $Extension,
                                     $MimeType,
                                     $Encoding,
                                     $Warnings,
                                     $StreamIds);

echo '<div style='overflow:auto; width:1000px; height:700px'>';
echo $result_html;
echo '</div>';

Note the use of the ReplacementRoot property on the RenderAsHTML type: its value is used to replace all links in the generated report with relative links to the PHP page you are using. Here is the getPageURL function that returns the current page URL:

function getPageURL()
{
    $PageUrl = $_SERVER["HTTPS"] == "on"? '
https://' : 'http://';
    $uri = $_SERVER["REQUEST_URI"];
    $index = strpos($uri, '?');
    if($index !== false)
    {
         $uri = substr($uri, 0, $index);
    }
    $PageUrl .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $uri;
    return $PageUrl;
}

Note: See this post for an example of rendering a report as an Excel document: http://blogs.msdn.com/b/brian_swan/archive/2010/09/23/rendering-sql-server-reports-as-excel-documents-with-php.aspx.

Really, that’s it…pretty simple. (Attached to this post is a file with the complete source code.) Of course, depending on the nature of your report, you might have to write a bit more code to handle postback events, but the basics are here. I think the Interoperability team did a great job in designing the SDK…it really makes consuming and displaying reports straightforward from PHP. To dig deeper into the capabilities of the SDK, read the docs that come with the download. Or, if you have questions, ask in a comment here and I’ll do some homework.

Thanks!

-Brian

Share this on Twitter

Attachment: ssrs_demo.php
Leave a Comment
  • Please add 4 and 8 and type the answer here:
  • Post
  • Hi Brian!

    Thanks for this very useful information it helps a lot :)

    But i'm having a problem here with my application, i've done everything like you exemplified here but still having this problem with the collapse/expand functionality.

    The report its called and i'm able to collapse/expand the groups, but the +/- icon are asking the credentials to load, its like they are being retrieved from other connection. It wasn't suppose  to load the icons in the same connection that the report is being retrieved? Am I doing something wrong?

    Thanks in advance,

    Regards

  • Hmmm...so the report renders, but you have to provide credentials to expand/collapse a category (and if you provide the PHPDemoUser credentials, you *can* expan/collapse a category). Do I have that right?

    I'm not sure this is relevant, but what authentication mode are you using for your website? (Anonymous? Windows?)

    -Brian

  • Yes the report renders, but it ask credentials just to load the -/+ icons. This is with a user that i create with a test environment that i've created.

    I've configured everything like the manual explain, so use "basic authentication".

    I've reported this issue to the SDK project to, they are investigating now this behavior.

    I've make a workaround on this problem, it appears that it calls a link to the +/- icons and that link are asking for the credentials to be given. So i replace that call with a call to a +/- signs of mine in the PHP file, so it no longer needs another credentials to be given. It works but its not straightforward, but I'll wait for the answer of the SSRS PHP SDK development team.

    Thankz

  • hello

    sorry for my English

    posted this question in the forum of microsoft and I did not have the answer to my question

    I try to use the SDK for SSRS php.

    but I could not progress because of this error:

     my report generator works on IE6

    but under IE8  I am asked a password and a user name

    I tried everything but nothing works

    What should I do?

    I use SSRS 2008.

    when I type the URL (http://ESI-933B4EF0C81:8080/Reports/Pages/Folder.aspx)

    in IE8.

     I get an image like this.

    (neumann.hec.ca/.../authentification.gif )

    I do not know what to put as password and username.

    knowing that I am using windows authentication in sql server 2008 for instance MSSQL.

    and I use an account for sql server instance SQLEXPRESS

    thank you and if you want further clarification you said to me

  • That sounds like a permissions issue rather than a problem with the SDK. A couple of things you might try:

    1. Try running IE with Administrator privileges.

    2. Try changing the credentials under which your website's application pool is running.

    -Brian

  • Hello,

    RESOLVED

    so I managed to post the report in IE8

    I went to internet option and then IE6 security.

    I copied the titles that have been checked.

    then I installed IE8.

    I checked the same titles as those on IE6.

    then under IE8 I type the following URL (http://ESI-933B4EF0C81:8080/Reports/Pages/Folder.aspx).

    then right-click the yellow band and then I clicked on (enable intranet setting).

    thank's

  • Hello,

    is it possible to work with the SDK under wamp.

    and if possible then how????

    thank's

  • It isn't possible to use the SDK with WAMP - specifically not with MySQL. The SDK is designed to work with SQL Server Reporting Services (which is specific to SQL Server). However, I do know that some developers have linked a SQL Server instance to their MySQL database and then used SSRS to generate reports on the SQL Server data. A quick search will turn up lots of information about creating a linked server: http://www.bing.com/search?q=create+linked+server&form=QBRE&qs=n&sk=&sc=2-20.

    I hope that helps. Let me know if you have more questions.

    -Brian

  • I already managed to connect to sql server wamp server.

    just uncomment this line (; = php_mssql.dll extension) in php.ini in wamp server

    I often wamp applications with SQL Server 2008 DB

    now the question is for the SDK + wamp???

    thank you

  • if it is not possible with wamp sooffer me something else that IIS7.

    because I'm under xp version 2002.and with SQL Server 2008.

  • Ah...I see. I usually think WAMP = Windows/Apache/MySQL/PHP. I didn't understand that you were using a SQL Server database.

    You should be able to create a report by following the steps in this post: blogs.msdn.com/.../getting-started-with-sql-server-reporting-services-ssrs.aspx. Then you can follow the steps in the post above to access reports. Give it a try and see how it goes.

    Note that the SDK does not depend on a database driver (like php_mssql.dll or php_sqlsrv.dll) - it accesses reports via the Report Server web service.

    -Brian

  • you know I managed to view the reports.

    BI with SQL Server.

    but it hemstitch I try I can not manage with wamp.

    I get errors that appears.

    eg I am asked to add the ssrsreport.php

    in helloworld.php.

    so I copied all the file (bin) I added to my file (.. / helloworld / code)

    then I put it all in the folder (www) from wamp.

    I even turned to soap wamp server.

    I also change (app.config).

    I do not know what the problem is just credentials.

    then I do not know what to put in credentials

    if not maybe the problem lies elsewhere????

    because I do not know or have begun I will like to give me step by step with wamp

    thank you

  • Hello,

    I followed the steps in your tutorial.

    I show you what I get.

    ////////////////////////////////////////////////////////////////

    SSRSReportException Object

    (

       [errorCode] =>

       [errorDescription] => Failed to connect to Reporting Service   Make sure that the url (http://esi-d01dc5a0500:8080/Reports/) and credentials are correct!

       [soapFault] =>

       [message:protected] =>

       [string:private] =>

       [code:protected] => 0

       [file:protected] => C:\wamp\www\Code\SSRSReport.php

       [line:protected] => 197

       [trace:private] => Array

           (

               [0] => Array

                   (

                       [file] => C:\wamp\www\Code\test.php

                       [line] => 12

                       [function] => SSRSReport

                       [class] => SSRSReport

                       [type] => ->

                       [args] => Array

                           (

                               [0] => Credentials Object

                                   (

                                       [_userName:private] =>

                                       [_password:private] =>

                                   )

                               [1] => http://esi-d01dc5a0500:8080/Reports

                           )

                   )

           )

    )

    ).

    ///////////////////////////////////////////////////

    is it possible to tell me why there's nothing that appears

    please also note that I did not create.

    and I go to sql server 2008 using windows authentication.

    thank's

  • Hello,

    where and how you created the PHPDemoUser and password of PHPDemoUser ?

    Thank's

  • Hello

    I run the SQL Server Management Studio.

    then I'm right on security.

    then new connection.

    in access name I typed (PHPDemoUser).

    then I typed the password.

    then I created a new user named (PHPDemoUser)in the db (AdventureWorks2008).

    and when I tried to add (PHPDemoUser) in the report generator get the following error (The name of the user or group 'PHPDemoUser' is not recognized. (rsUnknownUserName))

    thank you kindly respond

Page 1 of 4 (48 items) 1234