PHP的-file_get_contents()与字符串变量?

问题描述:

我正在尝试在收到此订单时使用PHPMailer通过电子邮件将订单发送给客户. 我尝试过这种方式:

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

我想以字符串形式获取此页面的内容,以便可以使用PHPMailer发送此内容,但是由于其中包含变量$ 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?

仅当将file_get_contents与URL感知流包装一起使用时,才能添加查询参数.它适用于http://localhost/yourfile.php?foo=bar.这样做会使用指定的查询参数向本地主机上的Web服务器发出HTTP Get请求.该请求将被处理,并返回请求的结果.

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.

仅将file_get_contents与文件名一起使用时,不会有任何HTTP请求.该呼叫将直接转到您的文件系统.您的文件系统不是Web服务器. PHP将仅读取文件内容.它不会执行PHP脚本,只会返回其中的文本.

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.

您应该 include 并调用任何脚本手动执行.如果脚本取决于order参数,请在包含文件之前通过$_GET['order'] = $orderId进行设置.