Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - file_get_contents() with variable in string?

Im trying to email an order with PHPMailer to the customer when this order is received. I tried doing that this way:

        $email_page = file_get_contents("email_order_html.php?order=$order_id");

I want to get the contents of this page as string so I can send this with PHPMailer but this function wont execute the page because of the variable $order_id in it, how can I fix this?

like image 307
Ryan Avatar asked Dec 06 '22 20:12

Ryan


1 Answers

You can only add Query Params when using file_get_contents with an Url Aware Stream Wrapper, e.g. it would work for http://localhost/yourfile.php?foo=bar. Doing so would issue an HTTP Get Request to a webserver at localhost with the specified query params. The request would be processed and the result of the request would be returned.

When using file_get_contents with a filename only, there wont be any HTTP Requests. The call will go directly to your file system. Your file system is not a webserver. PHP will only read the file contents. It wont execute the PHP script and will only return the text in it.

You should include the file and call whatever the script does manually. If the script depends on the order argument, set it via $_GET['order'] = $orderId before including the file.

like image 73
Gordon Avatar answered Dec 10 '22 13:12

Gordon