Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CURLOPT_POST vs CURLOPT_CUSTOMREQUEST

Tags:

php

curl

I am using the Email on Acid API.

While sending POST requests to the endpoint http://sandbox.emailonacid.com/v4/GetClientList through PHP Curl, I tried the following alternatives :

  1. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST")
  2. curl_setopt($ch, CURLOPT_POST, 1);

The first option works, but the second option returns an HTTP 400 Bad Request.

Can anyone explain the behavior?

like image 408
Bharat Khatri Avatar asked Oct 21 '15 11:10

Bharat Khatri


People also ask

What is Curlopt_customrequest?

This option can be used to specify the request: HTTP. Instead of GET or HEAD when performing HTTP based requests. This is particularly useful, for example, for performing an HTTP DELETE request.

What is Curlopt_post?

CURLOPT_POST. true to do a regular HTTP POST. This POST is the normal application/x-www-form-urlencoded kind, most commonly used by HTML forms.

What is cURL setopt?

The curl_setopt() function will set options for a CURL session identified by the ch parameter. The option parameter is the option you want to set, and the value is the value of the option given by the option. The value should be a long for the following options (specified in the option parameter):


1 Answers

By default CURLOPT_POST will perform a regular POST request using a Content-Type header of "application/x-www-form-urlencoded" which is typically the kind used when submitting HTML forms.


The API you're consuming has restricted Accept headers for application/json or application/xml depending on whether your sending a JSON or XML payload.

You can override the default Content-Type for a regular POST header using:

$headers = ['Content-Type: application/json'];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
like image 107
paperclip Avatar answered Sep 19 '22 13:09

paperclip