2012-11-14 109 views
0

可能重複:
PHP + curl, HTTP POST sample code?
POST data to URL php
How to issue HTTP POST request?如何從PHP代碼中輕鬆創建POST?

我創建了攔截規則後的腳本,並基於用戶名只做一件事或其他。

在其中一個條件分支中,我想要將用戶一起移動,並將他在登錄表單中記下的內容發佈到另一個PHP腳本。

如何輕鬆地向URL發出POST請求?

有什麼樣:

$data = array("username" => "admin", "password" => "hunter2"); 

http_post("foo.com", "POST", $data); 

有什麼建議?

+0

你怎麼樣使用捲曲? – Ibu

+2

詢問前請先搜索。有[很多很多問題](http://stackoverflow.com/search?q=%5Bphp%5D+post+to+url)直接回答這個問題。 –

+0

有沒有一個圖書館真的爲我抽象呢? cURL看起來像是矯枉過正。 – sergserg

回答

4

像這樣:

// Create map with request parameters 
$params = array ('surname' => 'Filip', 'lastname' => 'Czaja'); 

// Build Http query using params 
$query = http_build_query ($params); 

// Create Http context details 
$contextData = array (
      'method' => 'POST', 
      'header' => "Connection: close\r\n". 
         "Content-Length: ".strlen($query)."\r\n", 
      'content'=> $query); 

// Create context resource for our request 
$context = stream_context_create (array ('http' => $contextData)); 

// Read page rendered as result of your POST request 
$result = file_get_contents (
       'http://www.sample-post-page.com', // page url 
       false, 
       $context); 

// Server response is now stored in $result variable so you can process it 

來源: http://fczaja.blogspot.ch/2011/07/php-how-to-send-post-request-with.html

+0

這看起來比cURl好,至少比較容易閱讀。我會試一試。 – sergserg

相關問題