Advertisement
Advertisement


POST data to a URL in PHP


Question

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.

2015/09/23
1
94
9/23/2015 8:38:26 PM

Accepted Answer

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.

2016/03/22
194
3/22/2016 10:55:29 AM


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

2010/06/20

Source: https://stackoverflow.com/questions/3080146
Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Email: [email protected]