Home >Backend Development >PHP Tutorial >How Can I Send POST Data to Web Pages Using cURL?
Passing POST Data to Pages using cURL
cURL is a powerful tool for making HTTP requests, allowing you to interact with web pages and services programmatically. One common task is passing POST data, which is used for submitting data to forms. Here's how to accomplish this with cURL:
Solution:
$data = array('name' => 'Ross', 'php_master' => true); // You can POST a file by prefixing with an @ (for <input type="file"> fields) $data['file'] = '@/home/user/world.jpg'; $handle = curl_init($url); curl_setopt($handle, CURLOPT_POST, true); curl_setopt($handle, CURLOPT_POSTFIELDS, $data); curl_exec($handle); curl_close($handle);
Explanation:
Data Encoding Options:
cURL provides two options for encoding POST data:
To encode as a string, use http_build_query($data) as the value for CURLOPT_POSTFIELDS.
References:
The above is the detailed content of How Can I Send POST Data to Web Pages Using cURL?. For more information, please follow other related articles on the PHP Chinese website!