Curl Json returning strange characters, unable to save to database

Asked

Viewed 38 times

1

I would like some help and I apologize if there is already an answer to this question here.

Thus:

  1. If I execute the command below via SSH::

    curl -X POST \
  https://pos-api.XXXXXXX.com.br/oauth/token \
  -H 'cache-control: no-cache' \
  -H 'content-type: application/x-www-form-urlencoded' \
  -d 'client_id=scott&client_secret=tiger&grant_type=password&username=merchantOne&password=keepitsecret'

Return me, CORRECTLY, something like in the example below: { "access_token": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "token_type": "bearer", "expires_in": 3599, "Scope": "trust read write" }

  1. HOWEVER, when I organize the code below in PHP, having the same goal of the command I posted there at the beginning:

$url = "https://pos-api.XXXXXXXXX.com.br/oauth/token";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$headers = array(
   "Accept: application/json",
   "Content-Type: application/json",
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

$data = '{"client_id":'.$client_id.',"client_secret":'.$client_secret.', "grant_type":'.$grant_type.',"username":'.$username.',"password":'.$password.'}';

curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

//for debug only!
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

$resp = curl_exec($curl);
curl_close($curl);
$token_gerado = var_dump($resp);

I receive as an answer something like:

string(112) "�d�K � F��c!��t-@�R>��h�ٸ�w87�Tه�&��5ƚYK���*�(��� �����o��C��wҧ��"����^l9n" Dados atualizados com suesso!

Also, I would like to organize the return that comes without the error and save in the database: { "access_token": "ab04421d-10d4-462c-b370-6456lrk897564aa48", "token_type": "bearer", "expires_in": 3599, "Scope": "trust read write" }

In this case, these commands correspond to the documentation available here: https://developer.ifood.com.br/docs/primeiros-passos-com-ifood-api

  • I was able to solve by following the tips of the following question: https://answall.com/questions/445909/como-fazer-curl-x-post-d-em-php

No answers

Browser other questions tagged

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