Advertisement
Guest User

mzukowski

a guest
Nov 12th, 2008
425
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.12 KB | None | 0 0
  1. <?php
  2. /**
  3.  * PHP client for requesting reports from JasperServer via SOAP.
  4.  *
  5.  * USAGE:
  6.  *
  7.  *  $jasper_url = "http://jasper.example.com/jasperserver/services/repository";
  8.  *  $jasper_username = "jasperadmin";
  9.  *  $jasper_password = "topsecret";
  10.  *
  11.  *
  12.  *  $client = new JasperClient($jasper_url, $jasper_username, $jasper_password);
  13.  *
  14.  *  $report_unit = "/my_report";
  15.  *  $report_format = "PDF";
  16.  *  $report_params = array('foo' => 'bar', 'fruit' => 'apple');
  17.  *
  18.  *  $result = $client->requestReport($report_unit, $report_format,$report_params);
  19.  *
  20.  *  header('Content-type: application/pdf');
  21.  *  echo $result;
  22.  */
  23.  
  24. class JasperClient {
  25.   private $url;
  26.   private $username;
  27.   private $password;
  28.  
  29.   public function __construct($url, $username, $password) {
  30.     $this->url = $url;
  31.     $this->username = $username;
  32.     $this->password = $password;
  33.   }
  34.  
  35.   public function requestReport($report, $format, $params) {
  36.     $params_xml = "";
  37.     foreach ($params as $name => $value) {
  38.       $params_xml .= "<parameter name=\"$name\"><![CDATA[$value]]></parameter>\n";
  39.     }
  40.    
  41.     $request = "
  42.      <request operationName=\"runReport\" locale=\"en\">
  43.        <argument name=\"RUN_OUTPUT_FORMAT\">$format</argument>
  44.        <resourceDescriptor name=\"\" wsType=\"\"
  45.        uriString=\"$report\"
  46.        isNew=\"false\">
  47.        <label>null</label>
  48.        $params_xml
  49.        </resourceDescriptor>
  50.      </request>
  51.    ";
  52.    
  53.     $client = new SoapClient(null, array(
  54.         'location'  => $this->url,
  55.         'uri'       => 'urn:',
  56.         'login'     => $this->username,
  57.         'password'  => $this->password,
  58.         'trace'    => 1,
  59.         'exception'=> 1,
  60.         'soap_version'  => SOAP_1_1,
  61.         'style'    => SOAP_RPC,
  62.         'use'      => SOAP_LITERAL
  63.  
  64.       ));
  65.  
  66.     $pdf = null;
  67.     try {
  68.       $result = $client->__soapCall('runReport', array(
  69.         new SoapParam($request,"requestXmlString")
  70.       ));
  71.      
  72.       $pdf = $this->parseReponseWithReportData(
  73.         $client->__getLastResponseHeaders(),
  74.         $client->__getLastResponse());
  75.     } catch(SoapFault $exception) {
  76.       $responseHeaders = $client->__getLastResponseHeaders();
  77.       if ($exception->faultstring == "looks like we got no XML document" &&
  78.           strpos($responseHeaders, "Content-Type: multipart/related;") !== false) {
  79.         $pdf = $this->parseReponseWithReportData($responseHeaders, $client->__getLastResponse());
  80.       } else {
  81.         throw $exception;
  82.       }
  83.     }
  84.    
  85.     if ($pdf)
  86.       return $pdf;
  87.     else
  88.       throw new Exception("Jasper did not return PDF data. Instead got: \n$result");
  89.   }
  90.  
  91.   protected function parseReponseWithReportData($responseHeaders, $responseBody) {
  92.     preg_match('/boundary="(.*?)"/', $responseHeaders, $matches);
  93.     $boundary = $matches[1];
  94.     $parts = explode($boundary, $responseBody);
  95.      
  96.     $pdf = null;
  97.     foreach($parts as $part) {
  98.       if (strpos($part, "Content-Type: application/pdf") !== false) {
  99.         $pdf = substr($part, strpos($part, '%PDF-'));
  100.         break;
  101.       }
  102.     }
  103.    
  104.     return $pdf;
  105.   }
  106.  
  107. }
  108. ?>
  109.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement