Request Curl Post with PHP

Asked

Viewed 2,362 times

0

How do I request in PHP?

curl -X POST "http://api.urlapi.com.br" -H "accept: application/json" -H "Authorization: xxxxxxxxxxxxxxxxxxxxxxxxxxxxx" -H "Content-Type: application/json" -d "{ \"menssage\": \"Aqui envio a mensagem\", \"number\": \"55123456789\"}"

1 answer

2


Using the CURL library itself would look like this:

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'http://api.urlapi.com.br');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{ \"menssage\": \"Aqui envio a mensagem\", \"number\": \"55123456789\"}");

$headers = array();
$headers[] = 'Accept: application/json';
$headers[] = 'Authorization: xxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close($ch);

However I recommend you use the Guzzle library, it is one of the most used to make requests, the documentation is well explained: http://docs.guzzlephp.org/en/stable/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.