I need to get a PIN code through an API but I can't get the Body of an End-Point response. The process is very simple and requires no authentication. Just send the email in the MAILTO field and the webservice returns the pin, or says that the email was not found. In my case below, I just want to receive the Json. But I can't do that.
However, when I test the endpoint in Postman, it works perfectly. But I can't when I try in PHP, even getting the CURL generated by Postman itself.
Here's the PHP code I'm using.
<?php
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://api-dev.meucliente.app.br/api/Login/sistema/[email protected]');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, true);
$esposta = curl_exec($curl);
curl_close($curl);
header('Content-Type: application/json');
var_dump($esposta);
?>
The above script returns the following result:
string(123) "HTTP/1.1 404 Not Found
Date: Thu, 24 Nov 2022 14:28:51 GMT
Content-Length: 0
Connection: keep-alive
Server: Kestrel
When I test the same end-point in Postman, the return is correct:
[
{
"Mensagem": "Email não foi encontrado!!",
"InnerException": null
}
]
I will be very grateful to whoever helps me.
CodePudding user response:
You did not include your Postman settings and the problem is there. Actually the problem is in your cURL request that does not mimic the Postman request. By default, cURL does GET
request but the API you talking to apparently expects POST
type of request and simply drops all request to that endpoint that do not meet that expectation. If you change your Postman method type to GET
and try to do the request then you would get no JSON back as well, same you faced with your PHP code.
So if that's the only problem you have, you need to also do POST
request and to do that just add:
curl_setopt($curl, CURLOPT_POST, true);
to your code and should be good:
<?php
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://api-dev.meucliente.app.br/api/Login/sistema/[email protected]');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_POST, true);
$esposta = curl_exec($curl);
curl_close($curl);
header('Content-Type: application/json');
var_dump($esposta);
which would return expected data:
string(167) "HTTP/2 400
date: Fri, 25 Nov 2022 05:11:11 GMT
content-type: application/json
server: Kestrel
[{"Mensagem":"Email não foi encontrado!!","InnerException":null}]"
BTW: the line
header('Content-Type: application/json');
is incorrect. You are not outputting plain JSON but just string (as per var_dump()
) so you should either have it removed, or remove curl_setopt($curl, CURLOPT_HEADER, true);
and replace var_dump()
with plain echo
. Or just replace header()
with echo '<pre>
;` if you test this in browser.
CodePudding user response:
First check if CURLOPT_URL is correct. and you can try some these examples:
<?php
// GET
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://example.com/api/Login/sistema/[email protected]');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
$esposta = curl_exec($curl);
curl_close($curl);
header('Content-Type: application/json');
echo($esposta);
?>
<?php
// PUT
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://example.com/api/Login/sistema/[email protected]');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
$esposta = curl_exec($curl);
curl_close($curl);
header('Content-Type: application/json');
echo($esposta);
?>
<?php
// POST
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://example.com/api/Login/sistema/forgot-pin');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_POSTFIELDS,http_build_query(['mailto' => '[email protected]']));
$esposta = curl_exec($curl);
curl_close($curl);
header('Content-Type: application/json');
echo($esposta);
?>