Home > Article > Backend Development > How to POST Data to a URL in PHP Without Using a Form?
Posting POST Data to a URL in PHP Without a Form
In PHP, posting data to a URL without utilizing an HTML form is achievable using curl. This method allows for sending variables to complete and submit a form.
Solution:
To post data to a URL using curl, follow these steps:
$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);
In this code:
curl_setopt() configures various curl options:
After executing curl_exec(), the response from the target URL is stored in $response. This response can be further processed as needed.
The above is the detailed content of How to POST Data to a URL in PHP Without Using a Form?. For more information, please follow other related articles on the PHP Chinese website!