POST data for URL in PHP

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

I am going to use it to submit a variable to fill out and submit a form.

+72
post php
Jun 20 '10 at 17:10
source share
3 answers

If you want to send data to the URL from the PHP code itself (without using the html form), this can be done using 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 post variables to the specified URL, and what the page returns will be in $ response.

+153
Jun 20 '10 at 17:32
source share

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); 
+53
Apr 30 '14 at 10:10
source share

Your question is not particularly clear, but if 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 like ,

+11
Jun 20 '10 at 17:29
source share



All Articles