0
I’m doing an integration with an api that brings real estate
In the documentation, it looks like this:
curl -X POST -d "client_id=1234&client_secret=abcd&grant_type=client_credentials" \
https://www.linkapi.com.br/oauth/token
However I do not know how to do in PHP, I did the way below, but returned empty({}bool(true)
):
$client_id="1234";
$client_secret="abcd";
$url ="https://www.linkapi.com.br/oauth/token";
$process = curl_init($url);
$data = json_encode(array('grant_type' => 'client_credentials', 'client_id' => $client_id, 'client_secret' => $client_secret));
curl_setopt($process, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
curl_setopt($process, CURLOPT_POST, true);
curl_setopt($process, CURLOPT_POSTFIELDS, $data);
$resp = curl_exec($process);
perfect, gave right, thanks! just for me to learn the urlencoded is because of the -X?
– Leandro Teraoka
e outra coisa, ele printou na tela {"access_token":"q95b","token_type":"Bearer","expires_in":31535701,"scope":"basic_info building_list building_details feedback","created_at":1586782560} como pego o acess_token?
– Leandro Teraoka
@Leandroteraoka That’s because of the
-d
, and due to the type of data being sent. It is a number of factors. If you were like-d '{"client_id": 1234, "client_secret": abc}'
would be a JSON, you acknowledge that this is JSON. While aclient_id=1234&client_secret=abcd
is a urlencoded, if it were to use the-F
would be amultipart/form-data
. But specifically about JSON, I would probably also use-H "Content-Type: application/json"
to overwrite theContent-Type
.– Inkeliz
@Leandroteraoka You must use the
CURLOPT_RETURNTRANSFER
so that the$resp
has the value of the answer. That’s,curl_setopt($process, CURLOPT_RETURNTRANSFER, true);
, edited the answer to include this.– Inkeliz
gave it right, thank you very much!
– Leandro Teraoka