Getting Started with PHP Application

To get start with the ReportViewer control using PHP wrapper classes, any one of the following prerequisites need to be installed in your machine to deploy, and run those samples locally.

In this section, you can see how to create, deploy, and run the ReportViewer samples using the Xampp server.

Creating a sample folder

Usually, the Xampp gets installed in C:\ drive. Now, create a new sample folder namely ReportViewerTutorial within C:\\xampp\\htdocs and place all the below mentioned folders within it.

  • Scripts - Includes all the script files necessary to render the control. [Optional, if cdn links are used in the sample]
  • CSS - Includes all the required stylesheet files. [Optional, if cdn links are used in the sample]
  • PHP class libraries - Includes the individual PHP wrapper class files for all controls. [Mandatory]
  • Sample PHP file (with .php extension). [Mandatory]

Adding scripts and CSS files

The required scripts and CSS files can be copied into the above created sample folder namely ReportViewerTutorial and can be manually referred on the sample page or else the cdn links can be referred directly. In case, if you are manually referring the scripts and CSS files in the PHP sample, refer this topic to know how to copy the required scripts and CSS from the installed location.

Adding PHP class libraries

Copy the PHP class libraries into the sample folder, which are the collection of PHP wrapper files. The PHP wrapper files are created individually for all controls to access and process its server-side values and then send it back to the client-side. These libraries are available within the following installed location:

  • (Installed Location)\Syncfusion\Essential Studio\17.3.0.9\PHP\Src

Create a PHP file

Create a first PHP file in Xampp and name it appropriately with .php extension and also place it under the newly created sample folder ReportViewerTutorial. For example, say Index.php with the initial code as shown below:

  • html
  • <!DOCTYPE html>
        <html>
            <head>
                <title>Getting Started - ReportViewer</title>
                <!--Dependency files references-->
            </head>
            <body>
                <?php
                ?>
            </body>
        </html>

    Scripts and CSS references

    Refer the required scripts and CSS files in the PHP page as mentioned below to render the ReportViewer control:

  • html
  • <!DOCTYPE html>
        <html>
            <head>
                <title>Getting Started - ReportViewer</title>
                <link href="http://cdn.syncfusion.com/17.3.0.9/js/web/flat-azure/ej.web.all.min.css" rel="stylesheet" />
                <link href="http://cdn.syncfusion.com/17.3.0.9/js/web/responsive-css/ej.responsive.css" rel="stylesheet" />
                <script src="http://cdn.syncfusion.com/js/assets/external/jquery-3.0.0.min.js"></script>
                <script src="http://cdn.syncfusion.com/js/assets/external/jsrender.min.js"></script>
                <script src="http://cdn.syncfusion.com/17.3.0.9/js/web/ej.web.all.min.js"></script>    
            </head>
            <body>
                <?php
                ?>
            </body>
        </html>

    AutoLoad file reference

    Include the PHP AutoLoad file reference within the body section of the PHP page.

  • html
  • <!DOCTYPE html>
        <html>
            <head>
               <title>Getting Started - ReportViewer</title>
               <link href="http://cdn.syncfusion.com/17.3.0.9/js/web/flat-azure/ej.web.all.min.css" rel="stylesheet" />
               <link href="http://cdn.syncfusion.com/17.3.0.9/js/web/responsive-css/ej.responsive.css" rel="stylesheet" />
               <script src="http://cdn.syncfusion.com/js/assets/external/jquery-3.0.0.min.js"></script>
               <script src="http://cdn.syncfusion.com/js/assets/external/jsrender.min.js"></script>
               <script src="http://cdn.syncfusion.com/17.3.0.9/js/web/ej.web.all.min.js"></script>     
            </head>
            <body>
                <?php require_once 'EJ\AutoLoad.php'; ?>
            </body>
        </html>

    Control initialization

    Create the ReportViewer control object by accessing the ReportViewer namespace EJ\\ReportViewer using the new keyword. Define its properties and then output the ReportViewer control by echoing the result object.

  • html
  • <body>
    <?php
        require_once 'EJ\AutoLoad.php';
        $ReportViewer = new EJ\ReportViewer("defaultReportViewer");
        echo $ReportViewer  -> width("100%") ->render();
    ?>
    </body>

    NOTE

    It is mandatory to define the render() method at last as given in the above syntax, to display the ReportViewer on the browser.

    Load Report Server Reports

    ReportViewer supports to load RDL files from the Report Server. The following steps help you to load reports from the Report Server.

    Set the Report Server ReportPath, ReportServiceUrl, and ServiceAuthorizationToken in the ReportViewer properties.

  • php
  • <?php
    
    $url = 'http://reportserver.syncfusion.com/api/get-user-key';
    $data = array('password' => 'demo', 'userid' => 'guest');
    
    $options = array(
        'http' => array(
            'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
            'method'  => 'POST',
            'content' => http_build_query($data),
        ),
    );
    $context  = stream_context_create($options);
    $result = file_get_contents($url, false, $context);
    $token = (json_decode($result, true)['Token']);
    $accessToken = (json_decode($token, true)['access_token']);
    $tokenType = (json_decode($token, true)['token_type']);
    
        $reportviewer = new EJ\ReportViewer('companysales_reportViewer');		
        echo $reportviewer->reportServiceUrl("http://reportserver.syncfusion.com/ReportService/api/Viewer")->serviceAuthorizationToken($tokenType. ' ' . $accessToken)->reportPath("/Sample Reports/Company Sales")->render();
    ?>

    NOTE

    In case, if we have to load the reportPath using guid instead of report location then we have a support to set guid of the report in reportPath as like as ‘reportPath(“91f24bf1-e537-4488-b19f-b37f77481d00”)’.

    NOTE

    To render reports from Report Server we need to generate ServiceAuthorizationToken with the help of server url and its credentials, Example: Url - http://localhost:53592/api/get-user-key and user name - User Name , password - Password. Use this live online server example: Url - http://reportserver.syncfusion.com/api/get-user-key and User name - guest , Password - demo.

    Running the PHP file

    The above created sample is now ready to run. Therefore, open the XAMPP control panel and start the Apache module as shown in the below image:

    Now, the sample can be run directly on the browser through localhost with appropriate port numbers, on which the Apache server is currently listening. For example, say if the Apache is configured to listen on port 8080, then type http://localhost:8080/ on the browser and press enter. Also, make sure that the sample folder is present within this location C:\\xampp\\htdocs as mentioned earlier.

    The following ReportViewer output shows up on the browser, when you type http://localhost:8080/ReportViewerTutorial/index.php and press enter.

    ReportViewer with Company Sales Report

    NOTE

    In case, if you face any problem with the default port 80 while running the sample, make the Apache to listen on some other different ports. The port number changes need to be done on both the httpd.conf and httpd-ssl.conf files, to get rid of this problem.(Refer here).