重定向到https时,uber CURL无法正常工作

I have quite the dilemma here, while i test on the Uber sandbox using the redirect uri to http://localhost the function below works just fine and returns the access token and i can use all their API calls just fine with no errors

BUT when i try to change the redirect URI to an API with https the function below does not work anymore, all info are received just like for the localhost but when the CURL is executed i get

{"error":"invalid_request"}

and the following code is the function i use for this:

function fetchUrl($url, $code)
{

$fields = array(
    'client_id' => "MY_CLIENT_ID",
    'client_secret' => "MY_CLIENT_SECRET",
    'grant_type' => "authorization_code",
    'code' => $code
);

$fields_string = '';
foreach ($fields as $key => $value) {
    $fields_string .= $key . '=' . $value . '&';
}

$fields_string = rtrim($fields_string, '&');

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//up to this point works for HTTPS redirect uri
$result = curl_exec($ch); //the second this runs, i get
//{"error":"invalid_request"}

curl_close($ch);
return $result;
}

for the function above:

the $url param is set to https://login.uber.com/oauth/v2/token and the $code is set to what i get after oauth goes trough and returns the necessary code (the code is received in 100% of the attempts).

Any help is appreciated! Thank you in advance!

I found a solution here,$code should only be used once to generate the access_token and the refresh_token that will be later used to refresh the access_token.

My mistake was to try to generate the token every time i used a method from uber, that is not needed, if you follow the steps from this link https://developer.uber.com/docs/riders/guides/authentication/user-access-token you can be sure it will work just fine!