2013-03-15 80 views
2

我在網站上有一個非常簡單的腳本。該腳本正在將HTTP POST發送到我可以控制的另一個站點。我知道如何使用cURL發送POST,但是我發送它的腳本必須對它做出響應?我要發送cURL帖子必須做的「響應」頁面是什麼?

例如,以下是發送POST的代碼(如果此腳本回顯來自站點B的響應,但我甚至不知道如何讓站點B響應該帖子) :

<?php 
// 
// A very simple PHP example that sends a HTTP POST to a remote site 
// 

$ch = curl_init(); 

curl_setopt($ch, CURLOPT_URL,"http://www.mysite.com/tester.phtml"); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, 
      "postvar1=value1&postvar2=value2&postvar3=value3"); 

// receive server response ... 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

$server_output = curl_exec ($ch); 

curl_close ($ch); 

echo $server_output; 

?> 

然後站點b,在後上述被髮送到URL,下面的代碼有:

<?php 

$postvar1 = $_POST['postvar1']; 

if ($postvar1="apples and oranges") { 
$echo "This is what you see if postvar1 has been posted and is equal to apples and oranges"; } else {echo "this is what you see if postvar1 has not been posted or is not equal to apples and oranges"; } 


?> 

什麼需要去上的腳本,在站點b與迴應基於腳本中的「if邏輯」出現在屏幕上的文本?

+0

你爲什麼這樣做'$ echo'? – ceejayoz 2013-03-15 15:13:34

回答

3

站點B的響應是發送的任何輸出。你在代碼中有一個錯誤:

$echo "This is what you see..." 

應該

echo "This is what you see..." 

我想這就是爲什麼你看不到任何回覆。

爲了查看該請求是否是成功的,你可以(在測試期間)在腳本的網站B.實例的開頭或結尾呼應了一些字符串:

<?php 
echo 'Hi, I\'m site B!<br/>'; 
$postvar1 = $_POST['postvar1']; 
+0

鷹眼人!應該是一個徽章。 – 2013-03-15 15:07:43

0

您只需執行正常的HTML輸出(或JSON或任何其他),就如同您將響應Web瀏覽器的普通POST一樣。

相關問題