Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

POST data to a URL in PHP

Tags:

post

php

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.

like image 773
Belgin Fish Avatar asked Jun 20 '10 17:06

Belgin Fish


People also ask

How POST URL in PHP?

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 .

How do you POST data into a URL?

POST request in itself means sending information in the body. I found a fairly simple way to do this. Use Postman by Google, which allows you to specify the content-type (a header field) as application/json and then provide name-value pairs as parameters.

How can I pass POST parameters in a URL?

You could use a button instead of an anchor and just style the button to look like a link. That way you could have your values in hidden fields inside the same form to be sent via POST. In addition to what Ben said, you can also let the link be a dummy and have it execute a javascript that submits a hidden form.

What is $_ POST in PHP?

PHP $_POST is a PHP super global variable which is used to collect form data after submitting an HTML form with method="post". $_POST is also widely used to pass variables. The example below shows a form with an input field and a submit button.


1 Answers

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.

like image 138
Peter Anselmo Avatar answered Sep 28 '22 05:09

Peter Anselmo