2015-10-16 77 views
-1

我試圖發送一個網頁作爲電子郵件。此網頁的內容基於變量,這些變量使用_GET方法調用。如何使用file_get_contents發送變量?

我有這部分的代碼,如果我沒有在網址中加入任何變量,但在我寫?variable=後立即回覆[function.file-get-contents]: failed to open stream: No such file or directory in...

<? 

$var_one = $_GET['var_one']; 
$var_two = $_GET['var_two']; 
$from = "Company name"; 
$from_email = "[email protected]"; 
$email_text = file_get_contents("mail_this_page.php?varone=".$var_one."&vartwo=".$var_two); 

$headers = "From:[email protected]"; 
$headers = "From: $from <$from_email>\r\n". 
      "MIME-Version: 1.0" . "\r\n" . 
      "Content-type: text/html; charset=UTF-8" . "\r\n"; 

mail('[email protected]', 'Hello', $email_text, $headers); 

?> 

所以,問題是在file_get_contents行。

$email_text = file_get_contents("mail_this_page.php"); 

作品,但

$email_text = file_get_contents("mail_this_page.php?varone=".$var_one."&vartwo=".$var_two); 

沒有。我究竟做錯了什麼?我怎樣才能使用這種方法傳遞變量?

+0

您是否看到http://stackoverflow.com/questions/7352832/get-request-from-php-using-file-get-contents-with-parameters? – FBHY

+0

是的,嘗試過使用單引號和雙引號等多種解決方案,都給出了相同的結果 –

回答

0

功能file_get_contents()以文件名的字符串作爲參數。然後這用於與文件系統一起檢查具有該確切名稱的文件。由於您傳遞的文件名並不以擴展名'.php'結尾,它正在尋找一個名稱包含'?'後的所有文件的文件。

它確實在尋找文件mail_this_page.php?varone = VAR_ONE & vartwo = VAR_TWO(假設變量是這樣評估的)在硬盤上。

+0

聽起來很合邏輯。我如何將它們作爲變量傳遞? –

+0

查看http://stackoverflow.com/questions/3488425/php-ini-file-get-contents-external-url,瞭解如何使用URL執行此操作 – gabe3886