JSON:如何解码到数组并使用值

时间:2017-01-03 16:25:08

标签: php jquery json

我正在玩一个使用json的支付系统。结果,用户被重定向到结帐页面。最后的PHP响应是这样的:

JSON:

{
  "id": 150, 
  "full_page_checkout": "https://swipe.lv/client/payment/979ffe8e8e74dfcce30c517aca07a5f848ddf8bf2e079080f8b623a2e59afdd8/full_page/", 
  "iframe_checkout": "https://swipe.lv/client/payment/979ffe8e8e74dfcce30c517aca07a5f848ddf8bf2e079080f8b623a2e59afdd8/iframe/", 
  "errors": [], 
  "timestamp": 1447668000
}

我的代码看起来像这样:

PHP:

session_start();

$name = $_POST['name'];
$email= $_POST['email'];
$tel = $_POST['tel'];
$adress = $_POST['adress'];
$city = $_POST['city'];
$plz = $_POST['plz'];
$how = $_POST['howtext'];
$quantity = $_POST['quantity'];
$dispatch = $_POST['dispatch'];
$payment = $_POST['payment'];
$price = $_POST['pricevalue'];
$comment = $_POST['comment'];


$public_key = '4838026a073b766840dbae4c2c5cbfab';
$private_key = '0000000000000000000000000000000000000000000000000000000';
$timestamp = (string)time();

$params = json_encode(array(
        'client' => array('email' => 'email@email.com'),
        'products' => array(
            array(
                'description' => 'Standard Cube',
                'price' => 20.45,
                'quantity' => 1
            ),
        )
    )
);

$authorization_message = $timestamp.' POST /api/v0.5/payments/ '.$params;
$authorization = hash_hmac('sha256', $authorization_message, $private_key);
$authorization_header = $public_key.','.$timestamp.','.$authorization;

$options = array(
    'http' => array(
        'header'  => "Content-type: application/json\r\nAuthorization: ".$authorization_header,
        'method'  => 'POST',
        'content' => $params,
        'ignore_errors' => true
    ),
);
$context = stream_context_create($options);
$result = file_get_contents('https://swipe.lv/api/v0.5/payments/ ', false, $context);
$link = substr($result,37,110);
echo json_encode($link);

为了执行链接,我使用了这样的解决方案:

$link = substr($result,37,110);
echo json_encode($link);

由于json根据值而变化,因此我无法使用substr函数。我想要做的是将jason转换为数组,而不是回显"full_page_checkout"

的值

我怎样才能做到这一点?

编辑:

通过使用json_decode并回显结果,我得到Object : object作为结果

0 个答案:

没有答案