Using Rest API 8 How to get access_token value in response

And also how to find current path url

$ch = curl_init();
$header = array(
‘Content-type: application/vnd.api+json’,
‘Accept: application/vnd.api+json’,
);
$postStr = json_encode(array(
‘grant_type’ => ‘client_credentials’,
‘client_id’ => ‘b2c95677-ca00-9b87-f7aa-5e27ec73c410’,
‘client_secret’ => ‘12345’,
‘scope’ => ‘standard:create standard:read standard:update standard:delete standard:delete standard:relationship:create standard:relationship:read standard:relationship:update standard:relationship:delete’,
));
$url = ‘http://stagingcrm.com/Api/access_token’;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, ‘POST’);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postStr);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$output = curl_exec($ch);

$response = json_decode($output, true);

var_dump( $response );

Hey,
pls have a look in \Api\docs\postman\ - there are postman examples that you can reuse to test against your environment.

From there, you can export the (hopefully working) request as php code as well, i.e. something like:

<?php
$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "<your system url>/Api/access_token",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "grant_type=client_credentials&client_id=<client-id>&client_secret=<client-secret>",
  CURLOPT_HTTPHEADER => array(
    "Content-Type: application/x-www-form-urlencoded"
  ),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;

No response i am getting blank page

1 Like

Have you set up everything accordingly to the documentation? Keys generated and such?

yes in the above code i have mention the same key

No, I’m referring to the public/private keys.

E: https://docs.suitecrm.com/developer/api/developer-setup-guide/json-api/#_before_you_start_calling_endpoints

composer is already instalIed on my system and i am confuse about what this cmd are telling i have run this cmd but i don’t know what that cmd is doing and also in system where can i find this directory {{suitecrm.root}}/Api/V8/OAuth2

you need to run that command from your installation folder, I guess there are some components missing within vendor/.

Further, the path you posted refers to the installation folder as well.

No i haven’t installed suitecrm on my system i am using one server suitecrm running on domain

Then you probably need a remote session (ssh) or do it on localhost and upload everything. Or use one of the older interfaces (soap/rest v4).

same problem with sopa/v4

is your code is working with your url
or you have install suitecrm as local

Code is working and tested with a regular webserver, not localhost.

can i see your code and which is working on server

I already pasted it - there is nothing different besides that I removed our credentials and the URL. If you want to use the v8 API, please follow the linked tutorial.