What does (-X, -H, -d) mean for this command (curl -X POST -H "Content-Type: application / json" -d) and how to do it from the php file?

2
curl -X POST -H "Content-Type: application/json" -d

I need to perform this command and send a JSON to a particular API that will return a response but I do not know how the structure of that would look in a PHP file and what that data means (-X -H -d). I've seen it look like this when we do the command with cmd .

    
asked by anonymous 30.07.2016 / 13:06

1 answer

2
curl -X POST -H "Content-Type: application/json" -d

According to the documentation:

  • -X : Specifies the request method ( GET ,% HEAD or POST ) to use when communicating with the server.

  • PUT : Indicates that a Header extra will be included in the request.

  • -H : Sends the specified data in a -d request to the server.

In PHP it would look something like this:

$curl = curl_init();
$url = "URL";

curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));

curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

$resultado = curl_exec($curl);
curl_close($curl);

print_r($resultado);

If you intend to send more data in this request, you must use the POST function to encode them properly. Here's an example:

$foo = "foo";
$bar = "bar";

$dadosJson = json_encode(array( "foo"=> $foo, "bar" => $bar));
curl_setopt($curl, CURLOPT_POSTFIELDS, $dadosJson);
    
30.07.2016 / 14:34