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?

2
  • 2
    Please search before asking. There are many, many questions that answer this directly. Commented Nov 14, 2012 at 22:15
  • Is there a library that REALLY abstracts this away for me? cURL seems like overkill. Commented Nov 14, 2012 at 22:16

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

1 Comment

This looks better than cURl, at least easier to read. I'll give it a try.
0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.