PHP cURL is a library that allows you to make HTTP requests in PHP. One of the most common uses of cURL is to make a POST request to a server, sending data to be processed. In this article, we will discuss how to use PHP cURL to make a POST request, including several examples of different types of data that can be sent in a POST request.
First, let's take a look at a basic example of how to make a POST request using cURL in PHP. The following code snippet shows how to send a simple string of data to a server using a POST request:
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://example.com/post-endpoint",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "data=Hello World!",
CURLOPT_HTTPHEADER => array(
"Content-Type: application/x-www-form-urlencoded"
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
?>
In this example, we are sending the string "Hello World!" to the endpoint "https://example.com/post-endpoint" using a POST request. Note that we are setting the "Content-Type" header to "application/x-www-form-urlencoded", which tells the server that we are sending data in the standard format for a web form.
You can also send JSON data in a POST request. Here is an example of how to send a JSON object as the data in a POST request:
<?php
$data = array(
"name" => "John Doe",
"email" => "johndoe@example.com"
);
$data_string = json_encode($data);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://example.com/post-endpoint",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $data_string,
CURLOPT_HTTPHEADER => array(
"Content-Type: application/json",
"Content-Length: " . strlen($data_string)
),
));
$response = curl_exec($curl);
Sure, in addition to sending data in a POST request, cURL also allows you to send files in a POST request. Here is an example of how to send a file in a POST request using cURL:
$file
);
curl_setopt_array($curl, array(
CURLOPT_URL => “https://example.com/post-endpoint”,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => “”,
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => “POST”,
CURLOPT_POSTFIELDS => $data,
CURLOPT_HTTPHEADER => array(
“Content-Type: multipart/form-data”
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
?>
In this example, we are sending a file located at '/path/to/file.jpg' to the endpoint "https://example.com/post-endpoint" using a POST request. Note that we are setting the "Content-Type" header to "multipart/form-data", which tells the server that we are sending a file. Additionally, we are using the CURLFile class to upload the file.
Another important aspect in cURL is the handling of response, when you perform a request to a server it returns a status code and a body, that can be JSON, plain text, HTML, etc. A common practice is to check the status code and then parse the body accordingly. Here is an example of how to handle the response:
“https://example.com/post-endpoint”,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => “”,
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => “POST”,
CURLOPT_POSTFIELDS => “data=Hello World!”,
CURLOPT_HTTPHEADER => array(
“Content-Type: application/x-www-form-urlencoded”
),
));
$response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
if($status == 200){
// parse json
$response_data = json
## Popular questions
1. What is cURL in PHP and why is it used?
cURL stands for “client for URLs” and is a library in PHP that allows you to make HTTP requests to other servers. It is commonly used to perform actions such as sending data in a POST request, downloading files, and making API calls.
2. How do you send data in a POST request using cURL in PHP?
You can send data in a POST request using cURL in PHP by setting the CURLOPT_POST option to true, and the CURLOPT_POSTFIELDS option to the data you want to send. Here is an example of how to send data in a POST request using cURL in PHP:
“`
“https://example.com/post-endpoint”,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => “data=Hello World!”,
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
?>
3. How do you send files in a POST request using cURL in PHP?
You can send files in a POST request using cURL in PHP by using the CURLFile class and adding the file to the post fields array. The server must have a endpoint that accept files as multipart/form-data. Here is an example of how to send a file in a POST request using cURL in PHP:
$file
);
curl_setopt_array($curl, array(
CURLOPT_URL => “https://example.com/post-endpoint”,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => “”,
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => “POST”,
CURLOPT_POSTFIELDS => $data,
CURLOPT_HTTPHEADER => array(
“Content-Type: multipart/form-data”
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
?>
4. How can you handle the response of a cURL POST request in PHP?
After executing the cURL request, you can use the curl_getinfo() function to retrieve the HTTP status code of the response. Based on the status code, you can determine if the request was successful or not. Additionally, you can use the curl_exec() function to get the body of the response. Here is an example of how to handle the response of a cURL POST request in PHP: