0

Possible Duplicate:
PHP + curl, HTTP POST sample code?
POST data to URL php
How to issue HTTP POST request?

I've created a script that intercepts a regular post, and based on the username does one thing or another.

In one of the conditional branches, I want to move the user along and POST what he wrote down in the login form to another PHP script.

How can I easily make a POST request to a URL?

Is there something like:

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

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

Any suggestions?

Community
  • 1
  • 1
sergserg
  • 19,010
  • 36
  • 118
  • 175

2 Answers2

6

Like this:

// 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

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

Zim84
  • 3,116
  • 2
  • 29
  • 37
0

That's what the curl functions exist for. See, for example: http://davidwalsh.name/curl-post

Konrad Neuwirth
  • 850
  • 5
  • 7