1

I am using a function to collect all information needed to build a report using mpdf, but to keep it simple I passed all values to a separate php file and then in the second php file it builds the html code which is passed back.

It is currently passed using the $_GET method, but now I am getting an error that the URI is to long...

Is there a way to pass all the values to the php file using the $_POST method without a from and a submit button? I want to just add it to the function not disrupting too much code...

Note:

using $_SESSION would not be an option, as I am calling the php file to build the function from a remote website with the code below, and using the result to build the pdf file...

Code:

$body = file_get_contents("http://mywebsite.com/templates/statement/body.php".);

Any assistance would be much appreciated.

Marcel
  • 752
  • 13
  • 26
  • http://stackoverflow.com/questions/2445276/how-to-post-data-in-php-using-file-get-contents then simply edit the remote `body.php` to access `$_POST` instead of `$_GET`. Though it would be easier to have the script run locally, it there a specific need for a remote script – Steve Feb 02 '16 at 13:20
  • @Steve the `file_get_contents` command means that I do not have to add the html code as a string, I can design the pdf file as I would any website html file, and then this command will build it into the pdf format... and I can change it to use the $_POST, but i am not sure how to post the information from within the function which builds it? – Marcel Feb 02 '16 at 13:23
  • OK, so thats why you are using an http request, so you can avoid echoing the html? I'll write you an answer with some magic in it! – Steve Feb 02 '16 at 13:24
  • @Steve exactly, even though the echo would be more effective. It will be much more difficult to both design and maintain – Marcel Feb 02 '16 at 13:25
  • See my answer, the http request is an unnecessary overhead. This is essentially how most php frameworks handle view templates – Steve Feb 02 '16 at 13:34
  • @SteveI tried Razvan's option first, because it had the least impact on my existing code, but thank you anyway :) – Marcel Feb 02 '16 at 14:15

2 Answers2

1

You can use curl to post to another page, if the curl extension is enabled in php.ini.

$params['pageHeader'] = "some header text";
$params['pageBody'] = "the page body";
$params['somethingElse'] = "other";
$postData = http_build_query($params); // look up http_build_query in the manual

$curlHandler = curl_init();
$url = "http://yourwebsite/post_to_mpdf.php";
curl_setopt($curlHandler, CURLOPT_URL,$url);
// yes, I'm posting alright
curl_setopt($curlHandler, CURLOPT_POST, true); 
curl_setopt($curlHandler, CURLOPT_POSTFIELDS, $postData);
curl_setopt($curlHandler, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curlHandler);

echo $response;
curl_close($curlHandler);
Răzvan
  • 886
  • 9
  • 19
  • would `$response` be the entire html website result? – Marcel Feb 02 '16 at 13:27
  • No. It "returns TRUE on success or FALSE on failure. However, if the CURLOPT_RETURNTRANSFER option is set, it will return the result on success, FALSE on failure." via the PHP man for curl_exec(); – Răzvan Feb 02 '16 at 13:33
  • Your option works but get's blocked on some servers by mod-security. – Marcel Feb 02 '16 at 21:16
1

You dont need to make an http request just to avoid echoing html.

You can just use output buffering instead:

template.php:

<html>
<head></head>
<body>
<h1><?php echo $title;?></h1>

<ul>
<?php foreach($items as $item):?>
    <li><?php echo $item;?></li>
<?php endforeach;?>
</ul>

</body>
<html/>

main.php:

$title = 'A title';
$items = ['one','two','three'];
//start output buffer
ob_start();
include 'template.php';
//capture output buffer as a string
$html = ob_get_clean(); //magic!

$html contains the following string:

<html>
<head></head>
<body>
<h1>A title</h1>

<ul>
    <li>one</li>
    <li>two</li>
    <li>three</li>
</ul>

</body>
<html/>
Steve
  • 19,825
  • 5
  • 37
  • 61