Created
November 18, 2008 18:32
-
-
Save gunark/26205 to your computer and use it in GitHub Desktop.
A simplified PHP version of a SOAP client for JasperServer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* PHP client for requesting reports from JasperServer via SOAP. | |
* | |
* USAGE: | |
* | |
* $jasper_url = "http://jasper.example.com/jasperserver/services/repository"; | |
* $jasper_username = "jasperadmin"; | |
* $jasper_password = "topsecret"; | |
* | |
* | |
* $client = new JasperClient($jasper_url, $jasper_username, $jasper_password); | |
* | |
* $report_unit = "/my_report"; | |
* $report_format = "PDF"; | |
* $report_params = array('foo' => 'bar', 'fruit' => 'apple'); | |
* | |
* $result = $client->requestReport($report_unit, $report_format,$report_params); | |
* | |
* header('Content-type: application/pdf'); | |
* echo $result; | |
*/ | |
class JasperClient { | |
private $url; | |
private $username; | |
private $password; | |
public function __construct($url, $username, $password) { | |
$this->url = $url; | |
$this->username = $username; | |
$this->password = $password; | |
} | |
public function requestReport($report, $format, $params) { | |
$params_xml = ""; | |
foreach ($params as $name => $value) { | |
$params_xml .= "<parameter name=\"$name\"><![CDATA[$value]]></parameter>\n"; | |
} | |
$request = " | |
<request operationName=\"runReport\" locale=\"en\"> | |
<argument name=\"RUN_OUTPUT_FORMAT\">$format</argument> | |
<resourceDescriptor name=\"\" wsType=\"\" | |
uriString=\"$report\" | |
isNew=\"false\"> | |
<label>null</label> | |
$params_xml | |
</resourceDescriptor> | |
</request> | |
"; | |
$client = new SoapClient(null, array( | |
'location' => $this->url, | |
'uri' => 'urn:', | |
'login' => $this->username, | |
'password' => $this->password, | |
'trace' => 1, | |
'exception'=> 1, | |
'soap_version' => SOAP_1_1, | |
'style' => SOAP_RPC, | |
'use' => SOAP_LITERAL | |
)); | |
$pdf = null; | |
try { | |
$result = $client->__soapCall('runReport', array( | |
new SoapParam($request,"requestXmlString") | |
)); | |
$pdf = $this->parseReponseWithReportData( | |
$client->__getLastResponseHeaders(), | |
$client->__getLastResponse()); | |
} catch(SoapFault $exception) { | |
$responseHeaders = $client->__getLastResponseHeaders(); | |
if ($exception->faultstring == "looks like we got no XML document" && | |
strpos($responseHeaders, "Content-Type: multipart/related;") !== false) { | |
$pdf = $this->parseReponseWithReportData($responseHeaders, $client->__getLastResponse()); | |
} else { | |
throw $exception; | |
} | |
} | |
if ($pdf) | |
return $pdf; | |
else | |
throw new Exception("Jasper did not return PDF data. Instead got: \n$pdf"); | |
} | |
protected function parseReponseWithReportData($responseHeaders, $responseBody) { | |
preg_match('/boundary="(.*?)"/', $responseHeaders, $matches); | |
$boundary = $matches[1]; | |
$parts = explode($boundary, $responseBody); | |
$pdf = null; | |
foreach($parts as $part) { | |
if (strpos($part, "Content-Type: application/pdf") !== false) { | |
$pdf = substr($part, strpos($part, '%PDF-')); | |
break; | |
} | |
} | |
return $pdf; | |
} | |
} | |
?> |
maxmaxmir
commented
Aug 2, 2010
_wsdlURL = str_replace("\\", "/", dirname(__FILE__)) . '/' . $config['Jasper']['wsdlURL'];
$this->_username = $config['Jasper']['username'];
$this->_password = $config['Jasper']['password'];
$this->_imageFolder = str_replace("\\", "/", dirname(dirname(__FILE__))) . '/public/images/';
try {
$this->_soapClient = new SOAPClient($this->_wsdlURL, array('login' => $this->_username,'password' => $this->_password,'trace' => 1,));
}
catch (Exception $e) {
throw $e;
}
}
//Methods
public function printReport($reportPath, $reportName, $outputFormat = "HTML", $parameterArray = "") {
$this->_reportPath = $reportPath;
$this->_reportName = $reportName;
$this->_outputFormat = $outputFormat;
$this->_parameterArray = $parameterArray;
$requestXML = "";
$requestXML .= "$outputFormat";
$requestXML .= "";
$requestXML .= "";
foreach ($parameterArray as $key=>$value) {
$requestXML .= "";
}
$requestXML .= "";
$params = array("request" => $requestXML );
$reportOutput = "";
try {
$response = $this->_soapClient->runReport($requestXML);
$reportOutput = $this->parseResponseWithReportData(
$this->_soapClient->__getLastResponseHeaders(),
$this->_soapClient->__getLastResponse(),
$outputFormat
);
}//end of try
catch (SoapFault $e) {
if ($e->faultstring == 'looks like we got no XML document') {
$reportOutput = $this->parseResponseWithReportData(
$this->_soapClient->__getLastResponseHeaders(),
$this->_soapClient->__getLastResponse(),
$outputFormat
);
}//end of if
else {
throw new Exception("Error Creating Report " . $e->faultstring);
}//end of else
}//end of catch
return $reportOutput;
}//end of function
private function parseResponseWithReportData($responseHeaders, $response, $outputFormat) {
preg_match('/boundary="(.*?)"/', $responseHeaders, $matches);
$boundary = $matches[1];
$parts = explode($boundary, $response);
$reportOutput = "";
switch ($outputFormat) {
case 'HTML':
foreach($parts as $part) {
if (strpos($part, "Content-Type: image/png") !== false) {
$start = strpos($part, "<") + 1;
$length = (strpos($part, ">") - $start);
$filename = substr($part, $start, $length) . '.png';
$file = fopen("$this->_imageFolder$filename","wb");
$contentStart = strpos($part, "PNG") - 1;
$contentLength = (strpos($part, "--") - $contentStart) + 1;
$contents = substr($part, $contentStart, $contentLength);
fwrite($file, $contents);
fclose($file);
}
if (strpos($part, "Content-Type: image/gif") !== false) {
$start = strpos($part, "<") + 1;
$length = (strpos($part, ">") - $start);
$filename = substr($part, $start, $length) . '.gif';
$file = fopen("$this->_imageFolder$filename","wb");
$contentStart = strpos($part, "GIF");
$contentLength = (strpos($part, "--") - $contentStart) + 1;
$contents = substr($part, $contentStart, $contentLength);
fwrite($file, $contents);
fclose($file);
}
if (strpos($part, "Content-Type: text/html") !== false) {
$contentStart = strpos($part, '');
$contentLength = (strpos($part, '') - $contentStart) + 7;
$reportOutput = substr($part, $contentStart, $contentLength);
}
}//end of for each
break;
case 'PDF':
foreach($parts as $part) {
if (strpos($part, "Content-Type: application/pdf") !== false) {
$reportOutput = substr($part, strpos($part, '%PDF-'));
break;
}
} //end of foreach
break;
case 'XLS':
foreach($parts as $part) {
if (strpos($part, "Content-Type: application/xls") !== false) {
$reportOutput = substr($part, (strpos($part, '') + 9));
break;
}
}
case 'CSV':
foreach($parts as $part) {
if (strpos($part, "Content-Type: application/vnd.ms-excel") !== false) {
$contentStart = strpos($part, 'Content-Id: ') + 24;
$reportOutput = substr($part, $contentStart);
break;
}
}
}
return $reportOutput;
}//end of functoin
```
}//end of class
ive created an improved soap client: http://snipt.net/b3ha/soapclienti-improved-soap-client-which-can-understand-a-mutliparted-message/
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment