在JSON多维数组中使用返回的结果

时间:2017-05-17 18:59:48

标签: javascript php json http

我有一个php文件“yotpo_authentication.php”,它与api交互以使用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];

?>

现在我的下一步是在我用来与api交互的第二个php文件中使用我被回复的用户令牌(access_token):

  <?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);

所以我相信我有2个问题:

  1. 我不确定我是否正确使用json_decode将json转换为php数组,或者我是否应该手动将json数据转换为php数组,因为它不是很长?
  2. 我不确定我是否正确地将从yotpo_authentication.php文件中检索到的access_token正确传递到第二个php文件json标记。

0 个答案:

没有答案