The PHP cURL is a library used for making HTTP requests. In order to use PHP cURL, you must have installed and enabled libcurl module for PHP on your system. In this tutorial, you will learn how to POST JSON data with PHP cURL requests. Basically, there are 4 steps involved to complete a cURL request using PHP.
Advertisement
- curl_init — The first step is to initializes a new session of cURL and return a cURL handle to other functions.
- curl_setopt — The second step is to set options for a cURL session handle. All these settings are very well explained at curl_setopt().
- curl_exec — In third step it perform a cURL session based on above options set.
- curl_close — The last step is to close a cURL session initialize by curl_init() and free all resources. Also deleted the cURL handle.
Let’s use the below sample code to create a POST request with PHP cURL.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | <?php // A sample PHP Script to POST data using cURL // Data in JSON format $data = array( 'username' => 'tecadmin', 'password' => '012345678' ); $payload = json_encode($data); // Prepare new cURL resource $ch = curl_init('https://api.example.com/api/1.0/user/login'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLINFO_HEADER_OUT, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $payload); // Set HTTP Header for POST request curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', 'Content-Length: ' . strlen($payload)) ); // Submit the POST request $result = curl_exec($ch); // Close cURL session handle curl_close($ch); ?> |
The main thing is that the request must be a POST request with properly json-encoded data in the body. The headers must properly describe the post body.
12 Comments
nice This was very helpful! Thanks for posting!
The main thing is that the request must be a POST request with properly json-encoded data in the body. The headers must properly describe the post body.
very helpful,
thank you
Is there a way to avoid the auto “Content-Type: application/x-www-form-urlencoded
” when using POST, I mean, I’m using the following code:
//—————————————————————————————————-
$ch = curl_init();
if(!empty($gdxParams))
{
$curlUrl=$curlUrl.’?’.$gdxParams;
}
$curlOptions=array();
if($gdxMethod==”POST”)
{
$curlOptions=[
CURLOPT_HTTPHEADER=>[
‘x-api-key: ‘.$gdxApiKey,
‘Authorization: Bearer ‘.$gdxApiKey,
‘Content-Type: application/json’,
],
CURLOPT_URL=>$curlUrl,
CURLOPT_CUSTOMREQUEST=>’POST’,
CURLOPT_POSTFIELDS=>json_encode($gdxArrayParams),
CURLOPT_RETURNTRANSFER=>true,
CURLINFO_HEADER_OUT=>true,
];
}
else//Method GET
{
$curlOptions=[
CURLOPT_HTTPHEADER=>[
‘x-api-key: ‘.$gdxApiKey,
‘Authorization: Bearer ‘.$gdxApiKey,
],
CURLOPT_URL=>$curlUrl,
CURLOPT_CUSTOMREQUEST=>’GET’,
CURLOPT_RETURNTRANSFER=>true,
CURLINFO_HEADER_OUT=>true,
];
}
curl_setopt_array($ch, $curlOptions);
$response = curl_exec($ch);
/*—*/
$arrayInfo=array();
$arrayInfo[‘header_info’]=curl_getinfo($ch, CURLINFO_HEADER_OUT);
$arrayInfo[‘response’]=$response;
var_dump($arrayInfo);
/*—*/
//————————————————————————————————
But the header info always gets set as:
//—————————————————————————————————————
‘header_info’ => string ‘POST /v1/reservations?tripId=XXXXXXXXXXXXXXXXXX&lang=es&verifyDuplicatedReservations=true HTTP/1.1
Host: stage.ws.gdx.travel
Accept: */*
x-api-key: XXXXXXXXXXXXXXXXXXXXXX
Authorization: Bearer XXXXXXXXXXXXXXXXXXXXXX
Content-Type: application/json
Content-Length: 221
Content-Type: application/x-www-form-urlencoded
//——————————————————————————————————
If I remove the CURLOPT_POSTFIELDS option, the headers only gets one “Content-Type” line (application/json, the one that I need)
Any clues?
it doesn´t work … boolean false is returned, while it works using Postman client
How to make a get request to the api using cURL
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
‘Content-Type: application/json’,
‘Content-Length: ‘ . strlen($payload))
);
You are asking for strlen($payload); which is the string-length while $payload is an array, so that gives an error?
Any advise?
IHAB,
json_encode turns the array into a JSON string, so strlen() will work.
Thanks a lot
This was very helpful! Thanks for posting!
Your all tutorilas are awesome bro. Plese provide some a big project in Core Php or CI .
How about radio buttons or select option?