Run report from JasperServer using PHP SOAP client

2019-04-02 06:20发布

问题:

I want to run a report from JasperServer using a PHP SOAP client. I found this example online, but I want to attach an XML data source, used for the report data, and I am unsure how it should be correctly attached.

How can I attach my XML data source to the SOAP request, that is acceptable to Jasper Server?

  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");
  }

The full example I found here https://gist.github.com/26205

The goal it to create something like this:

回答1:

This is more a comment than an answer, but probably helpful. There is a library called WSO2 WSF/PHP:

WSO2 WSF/PHP is intended to fill some of the gaps in the PHP extension. WSO2 WSF/PHP is an open source implementation like the SOAP extension and supports MTOM, WS-Addressing, WS-Security, and WS-RelaiableMessaging. WSO2 WSF/PHP supports a similar API to that of the SOAP extension. There are plans to wrap the API to provide the same API of the SOAP extension; it will be written in C.

I think you're looking for Binary attachment (MTOM).

The following links might be useful as well:

  • PHP SOAP Messages with AttachmentsPEAR::SOAP related
  • MIME Attachments using SoapClient ClassMailing List
  • SOAP Messages with AttachmentsW3C
  • PHP SOAP ExtensionIntroduction


回答2:

On their own site they have also some examples about integration to Web Services via php.

Is there any help for this?



回答3:

SOAP request has no attachment support.

The idea is how you process your request. The only way I use SOAP requests with attachments is to Base64 Encode the data to be attached, and add it to a Text node.

Add the tag with attribute encoded="true/false". If its a file content, supply the name of the file in the request.

In the server side, if you find the node with an attribute encoded="true", You can take the data from the node, Base64Decode it and do what ever you need.

The idea of Base64 is to avoid many special characters that a SOAP request doesn't support in Request. Some SOAP processors have the option with "encoded" attribute.



回答4:

You want to include a xml file in your SOAP query or response?

You could encode it base64 like in emails and then require the user on the other end to decode it.

$data = chunk_split(base64_encode($xml_data));

Then just add it in a seperate xml tag in your SOAP query/response.