I have one php file "yotpo_authentication.php" which is interacting with the api to get authenticated using curl:
<?php
$yotpo_keys = array(
"client_id"=> "myid",
"client_secret"=> "mysecret",
"grant_type"=> "client_credentials"
);
$ch = curl_init( "https://api.yotpo.com/oauth/token" );
# Setup request to send json via POST.
$payload = json_encode( $yotpo_keys );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $payload );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
# Return response instead of printing.
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
# Send request.
$result = curl_exec($ch);
curl_close($ch);
# Print response.
$yotpo_token_array = json_decode($result, true);
echo $yotpo_token_array[access_token];
?>
Now my next step is to use the user token that I am being given back (access_token) in a second php file that I am using to interact with the api:
<?php
require ('yotpo_authentication.php');
?>
<?php
$yotpo_platform = json_decode('{
"account_platform": {
"shop_domain": "http://testsitemm.com",
"platform_type_id": "2"
},
"utoken": '. $yotpo_token_array[access_token].'"
} ');
$ch = curl_init( " https://api.yotpo.com/apps/myapikey/account_platform" );
# Setup request to send json via POST.
$payload = json_encode( $yotpo_platform );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $payload );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
# Return response instead of printing.
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
# Send request.
$result = curl_exec($ch);
curl_close($ch);
# Print response.
$yotpo_token_array = json_decode($result, true);
So I believe I have 2 issues here:
- I am not sure if I am using json_decode correctly to convert json to a php array, or if I should manually turn the json data into a php array since it is not very long?
- I am not sure if I am correctly passing the access_token retrieved from the yotpo_authentication.php file correctly into the second php file json markup.