我有設置動作一個頁面上的表單來/process.php提交一臺服務器,並處理有關表單,然後發佈結果到另一個域
內process.php我有形式的驗證還寫入到它所在的服務器上的數據庫。
在寫入數據庫之後,我想要做的事情是將變量發佈到另一個域,然後以不同的方式處理這些變量。
我有設置動作一個頁面上的表單來/process.php提交一臺服務器,並處理有關表單,然後發佈結果到另一個域
內process.php我有形式的驗證還寫入到它所在的服務器上的數據庫。
在寫入數據庫之後,我想要做的事情是將變量發佈到另一個域,然後以不同的方式處理這些變量。
可以使用的file_get_contents爲同一。
例子:
$name="foobar";
$messge="blabla";
$postdata = http_build_query(
array(
'name' => $name,
'message' => $message
)
);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$result = @file_get_contents('http://yourdomain.com/process_request.php', false, $context);
if($http_response_header[0]=="HTTP/1.1 404 Not Found"):
echo "iam 404";
elseif($http_response_header[0]=="HTTP/1.1 200 OK"):
echo "iam 200";
else:
echo "unknown error";
endif;
謝謝你,它的工作原理。有沒有辦法從接收服務器接收發布的狀態? IE 200,500等? – Fraggy
與@符號,你可以抑制錯誤.'code' $結果= @ file_get_contents('http://yourdomain.com/process_request.php',假,$上下文);並檢查結果的狀態if($ result ==「FALSE」):處理錯誤else:處理返回值endif; – fortune
或者您可以使用「$ http_response_header」。
http://php.net/manual/en/reserved.variables.httpresponseheader.php –
fortune
示例使用curl:
$name = 'Test';
$email = '[email protected]';
$ch = curl_init(); // initialize curl handle
$url = "http://domain2.com/process.php";
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 10); // times out after 10s
curl_setopt($ch, CURLOPT_POST, 1); // set POST method
curl_setopt($ch, CURLOPT_POSTFIELDS, "text=$name&name=$email"); // post fields
$data = curl_exec($ch); // run the whole process
curl_close($ch);
例無捲曲:http://wezfurlong.org/blog/2006/nov/http-post-from-php-without-curl/
所以post字段,它會接受已經設置好的變量,例如$ name,$ email等 – Fraggy
當然!看一個例子。 – 2012-10-17 16:52:58
此CURL將發送郵件至domain2.com/process.php。 – 2012-10-17 16:55:32
你想用戶重定向到新的服務器呢?如果不是,我想'curl'可以成爲答案。 –
請你舉個例子。 「另一個域名」是什麼意思,那裏會發生什麼?你會等待那裏的迴應嗎? – enenen
你有什麼嘗試?我會從['curl'](http://www.php.net/manual/en/book.curl.php)開始...... – JKirchartz