2013-07-09 57 views
-2

我正在開發一個小程序Twilio service in PHP。 這裏是我的Twilio端代碼:爲什麼PHP include()不適用於Twilio?

<?php 
    header("content-type: text/xml"); 
    echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; 
?> 
<Response> 
<?php 
$currrentDate = date('Y-m-d', time()); 
$current = file_get_contents("1.txt"); 
$current .= "Caller (".$_REQUEST['From'].")"; 
file_put_contents("1.txt", $current); 
$current .= " at ".date("m/d/y G:i:s", time())."<br/>"; 
file_put_contents("1.txt", $current); 
?> 
</Response> 

這工作得很好,但是當我使用include函數這是行不通的。我把php代碼放到另一個名爲「test.php」的php文件中。

<?php 
    header("content-type: text/xml"); 
    echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; 
?> 
<Response> 
<?php 
include("test.php"); 
?> 
</Response> 

這裏是我的test.php文件。

<?php 
$currrentDate = date('Y-m-d', time()); 
$current = file_get_contents("1.txt"); 
$current .= "Caller (".$_REQUEST['From'].")"; 
file_put_contents("1.txt", $current); 
$current .= " at ".date("m/d/y G:i:s", time())."<br/>"; 
file_put_contents("1.txt", $current); 
?> 

有人能告訴我爲什麼嗎?謝謝!

+0

你能否解釋一下「不工作「更詳細?你預期會發生什麼,實際發生了什麼?我想知道是否有一些你應該提供的連接到Twilio的代碼 - 你可以編輯它嗎?只是你知道,「不起作用」的問題往往會被低估,因爲這不是一個非常有用的問題描述。 – halfer

+0

嗨,thx您的建議。實際上,當我撥打我的twilio號碼時,來電顯示將保存到一個txt文件中。我已經做到了。但是當我使用include函數來包含完全相同的代碼時,它將不起作用。 – nich

+0

嗯,你的第二項代碼輸出''標籤到標準輸出(Apache輸出到瀏覽器,我猜),但'file_put_contents'將內部內容重定向到一個過時的文件。所以,據我所知,你的迴應永遠是空的。不應該'test.php'將'$ current'的內容直接輸出到標準輸出? – halfer

回答

2

您發送的頭部和XML定義兩次..

刪除

<?php 
    header("content-type: text/xml"); 
    echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; 
?> 
<Response> 

?> 
</Response> 

從test.php的

+0

我沒有在我的第二個文件中的頭文件,只有幾個關於從主文件中寫入文件的代碼 – nich

+0

您的代碼示例在兩個文件中都有頭文件。 – user20232359723568423357842364

+0

我把我的test.php文件。那裏沒有標題。 – nich