0

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:

  1. 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?
  2. 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.
Sackling
  • 1,780
  • 5
  • 37
  • 71
  • Where's your javascript? Or maybe you're doing it all through PHP. Usually OAUTH2 access tokens are passed in the http header area rather than as a value in the body, but I guess that depends on the API you're accessing. – James May 17 '17 at 19:07
  • @James currently there is none. But I think it might be better to use jquery in the second file instead of curl. But I am not sure. – Sackling May 17 '17 at 19:11
  • Do you get a response on the second php file? An error? – James May 17 '17 at 19:15

0 Answers0