Skip to content

Instantly share code, notes, and snippets.

@webdevilopers
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save webdevilopers/c9e1b1f53a573cb0cfdd to your computer and use it in GitHub Desktop.
Save webdevilopers/c9e1b1f53a573cb0cfdd to your computer and use it in GitHub Desktop.
Client cURL multipart form-data POST to JasperReportsEngine Servlet
<?php
namespace Plusquam\Bundle\ContractBundle\Controller;
use Symfony\Component\HttpFoundation\Response;
class BundleController extends Controller
{
public function egzAction()
{
$servletExportService = $this->get('export_servlet');
$body = $servletExportService->outputFromServlet('pdf', 'egz', $options);
$filenameBase = "egz-bundle.pdf";
$response = new Response();
$response->headers->set('Content-Type', 'application/pdf');
$response->headers->set('Content-Disposition', 'inline; filename="' . $filenameBase . '"');
$response->setContent($body);
return $response;
}
}
<?php
use Guzzle\Http\Client;
class Servlet
{
public function postToServlet($url = '', $postParams)
{
$url = "http://localhost:8180/JasperReportsEngine/jasper";
$httpClient = new Client();
$request = $httpClient->post($url, array(
'Content-Type' => 'multipart/form-data'
), $postParams)
#->addPostFile('file', $postParams['data']) // how to add raw XML string instead of file / path?
;
#$response = $request->send();
$response = $request->send()->getBody(true); // get body only
return $response;
}
}
<?php
use GuzzleHttp\Client;
use GuzzleHttp\Post\PostFile;
class Servlet
{
public function postToServlet($url = '', $postParams)
{
$url = "http://localhost:8180/JasperReportsEngine/jasper";
$client = new Client();
$request = $client->createRequest('POST', $url);
$postBody = $request->getBody();
$postBody->replaceFields($postParams);
$postBody->addFile(new PostFile('data', $postParams['data']));
$response = $client->send($request)->getBody(true);
return $response;
}
}
<?php
namespace Export\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
class BundleController extends AbstractActionController
{
public function egzAction()
{
$servletExportService = $this->getServiceLocator()->get('ExportServletService');
$response = $servletExportService->outputFromServlet('pdf', 'egz', $options);
if (!$response->isSuccess()) {
// report failure
$message = $response->getStatusCode() . ': ' . $response->getReasonPhrase();
var_dump($response->getContent());
$response = $this->getResponse();
$response->setContent($message);
return $response;
}
$body = $response->getBody();
$filenameBase = "egz-bundle.pdf";
$response = $this->getResponse();
$response->setContent($body);
$headers = new \Zend\Http\Headers();
$headers->addHeaderLine('Pragma', 'cache');
$headers->addHeaderLine('Cache-control', 'private');
$headers->addHeaderLine('Content-Type', 'application/pdf');
$headers->addHeaderLine('Content-Disposition', 'inline; filename="' . $filenameBase . '"'); // Parse directely to browswer
$response->setHeaders($headers);
return $response;
}
}
<?php
use Zend\Http\Client;
class Servlet
{
public function postToServlet($url = '', $postParams)
{
$url = "http://localhost:8180/JasperReportsEngine/jasper";
$client = new Client(); // use ClientStatic? http://framework.zend.com/manual/2.0/en/modules/zend.http.client-static.html
$client->setAdapter('Zend\Http\Client\Adapter\Curl');
$client->setUri($url);
// How to set charset?
$client->setEncType(Client::ENC_FORMDATA); // Cannot handle content type '' automatically -> 'multipart/form-data'
$client->setMethod(Request::METHOD_POST);
$client->setParameterPOST($postParams);
$client->setFileUpload('data', 'data', $postParams['data'], 'application/xml');
$response = $client->send();
return $response;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment