102

How can I send POST data to a URL in PHP (without a form)?

I'm going to use it for sending a variable to complete and submit a form.

honk
  • 9,137
  • 11
  • 75
  • 83
Belgin Fish
  • 19,187
  • 41
  • 102
  • 131

3 Answers3

212

If you're looking to post data to a URL from PHP code itself (without using an html form) it can be done with curl. It will look like this:

$url = 'http://www.someurl.com';
$myvars = 'myvar1=' . $myvar1 . '&myvar2=' . $myvar2;

$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_POST, 1);
curl_setopt( $ch, CURLOPT_POSTFIELDS, $myvars);
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt( $ch, CURLOPT_HEADER, 0);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);

$response = curl_exec( $ch );

This will send the post variables to the specified url, and what the page returns will be in $response.

Norbert
  • 2,741
  • 8
  • 56
  • 111
Peter Anselmo
  • 2,961
  • 1
  • 17
  • 12
  • 2
    In as much as your solution is correct, I think the OP wanted to know how to do it with HTML form. Although the question was not very clear. – Helen Neely Jun 20 '10 at 17:49
  • We couldnt understand the problem but it locks the program. – albatross Dec 26 '13 at 14:45
  • 2
    care to elaborate what `CURLOPT_FOLLOWLOCATION`, `CURLOPT_HEADER` and `CURLOPT_RETURNTRANSFER` do? I prefer not to copy code I don't fully understand. – Stefan Fabian Jun 25 '16 at 13:22
  • @Stefan a 3 second Google search would pull up the php manual (which you should already be familiar with if you have a need for curl) and a clear explanation of all that: http://www.php.net/curl_setopt – Mike Sep 13 '16 at 05:38
  • 2
    @Mike while that is true I'd prefer to have everything in an answer to be either clear from the beginning or explained in the answer because people usually come to stackoverflow for an answer not to get more questions. – Stefan Fabian Sep 13 '16 at 08:21
  • 1
    @Stefan I felt the answer was perfectly clear... it addressed the question. If your level of understanding is below the level of the answer, then do some more research. Does he also need to explain that the `$url` has a `$` because in php that's how you indicate a variable? Where do you draw the line? "... not to get more questions" is not the attitude of someone that will succeed at self-learning, especially programming. – Mike Sep 22 '16 at 07:05
  • @Mike now you're getting ridiculous. Someone who wants to know how to POST data to an url probably knows the basics of php. However most of them probably did not know about curl. If they did, they probably wouldn't have come here. Any good teacher will tell you that you should always adjust your answer to the askers level of knowledge. Anyway I'll stop this discussion right here. stackoverflow comments are not the right place for debates on principles. – Stefan Fabian Sep 22 '16 at 10:00
  • @Stefan there are near hundreds of options for curl and those options will be different from case to case. What's provided in this answer is purely a generic solution to give enough direction to address a vague problem, which it does perfectly. The best teachers won't spoon feed you everything; proper learning requires some deeper research, trial and error. Finding the answer to a question with a 3 second Google search is hardly unreasonable. No need to reinvent the wheel. – Mike Sep 23 '16 at 06:13
81

cURL-less you can use in php5

$url = 'URL';
$data = array('field1' => 'value', 'field2' => 'value');
$options = array(
        'http' => array(
        'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
        'method'  => 'POST',
        'content' => http_build_query($data),
    )
);

$context  = stream_context_create($options);
$result = file_get_contents($url, false, $context);
var_dump($result);
Phd. Burak Öztürk
  • 1,727
  • 19
  • 29
9

Your question is not particularly clear, but in case you want to send POST data to a url without using a form, you can use either fsockopen or curl.

Here's a pretty good walkthrough of both

jfoucher
  • 2,251
  • 2
  • 21
  • 29