0

I'm trying to use the REST API of a telecommunications company to return an access token using curl, here is my code:

<?php

$curl = curl_init();

curl_setopt_array($curl, [
  CURLOPT_URL => "https://api.touch.technology/auth/token",
  CURLOPT_POST=> true,
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "grant_type=client_credentials&client_id=*********&client_secret=*********",
  "content-type: x-www-form-urlencoded",
]);
$data = curl_exec($curl);
curl_close($curl);
var_dump($data);

?>

Yet all i recive is "string(12) "Unauthorized""

Can anybody tell me where im going wrong?

:API Docs

1
  • "content-type: x-www-form-urlencoded" isn't a valid setopt option. Commented Feb 28, 2022 at 22:23

1 Answer 1

1

hope this helps, check the comments for explanation

<?php
$curl = curl_init();

curl_setopt_array($curl, [
  CURLOPT_URL => "https://api.touch.technology/auth/token",
  CURLOPT_POST=> true,
  CURLOPT_RETURNTRANSFER => true,
  //you have CURLOPT_POST set to true so don't need this
  //CURLOPT_CUSTOMREQUEST => "POST", 
  //CURLOPT_POSTFIELDS => "grant_type=client_credentials&client_id=*********&client_secret=*********",
  //don't need this
  //"content-type: x-www-form-urlencoded",
  //client secret and client_id must be url encoded
  CURLOPT_POSTFIELDS => "grant_type=client_credentials&client_id=".urlencode(*********)."&client_secret=".urlencode(*********)
]);
$data = curl_exec($curl);
curl_close($curl);
var_dump($data);

?>

you can try to use www.hurlit.com to test the API and make sure it works with the data you have (secret and key) and then try it out with curl.

Sign up to request clarification or add additional context in comments.

4 Comments

It's easier to use http_build_query() for the postfields
its true http_build_query() is easier @Phil thanks for the edit
@KaleshwarChand thank you for your answer however this returns another error: Fatal error: Uncaught TypeError: Unsupported operand types: string + string in C:\xampp\htdocs\MEMS\admin.php:19 Stack trace: #0
@Natan Rawlingson I have edited the code and it should work now I used "+" but it should have been "." sorry, confused js with php

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.