LLamar a una API externa con Guzzle (peticiones https) en vez de usar CURL

Ejemplo mandando por POST, con auth básica y adjuntando un JSON:
composer require guzzlehttp/guzzle
$client = new GuzzleHttp\Client();
$url = "https://pepitoperez/llamar_api?api_key=qNPzVxxxxxX7rw9l";
$json_orig = '{"lead": {
"properties": [
{
"property": "email",
"value": "pepitoperez@gmail.com"
},
{
"property": "nif",
"value": "xxxx"
}
]
}
}';

$response = $client->post($url, [
   'headers' => ['Content-type' => 'application/json'],
   'auth' => [
     'aaaa',
     'bbbb'
],
   'body' => $json_orig,
]);

return $response;

IMPORTANTE: Guzzle no te devuelve el json (o la respuesta que sea) en $response, sino para acceder a la respuesta es: $response->getBody()

No hay comentarios:

Publicar un comentario