从json字符串中选择一个特定的部分

时间:2016-07-20 17:01:52

标签: javascript php json curl

我正在尝试将结帐页面与支付网关连接起来。为此,我使用php Curl并生成响应。响应中包含各种付款细节,但我只需要选取存储在“longurl”中的网址进行重定向。

  

{ "success": true, "payment_request": { "id": "9f999fc8407d473c8c8f910e8398a527", "phone": "+917894561230", "email": "foo@example.com", "buyer_name": "", "amount": "798.00", "purpose": "ad", "status": "Pending", "send_sms": true, "send_email": true, "sms_status": "Pending", "email_status": "Pending", "shorturl": null, "longurl": "https://www.instamojo.com/@mghj/9f999fc8407d473c8c8f910e8398a527", "webhook": "http://www.example.com/webhook/", "created_at": "2016-07-20T16:53:12.349Z", "modified_at": "2016-07-20T16:53:12.349Z", "allow_repeated_payments": false }

<?php
$payer_name = $_POST['name'];
$amt = $_POST['net_amt'];
$amt = (int)$amt;
$payer_email = $_POST['email'];
$payer_no = $_POST['mobno'];
$payer_no = (int)$payer_no;

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://www.instamojo.com/api/1.1/payment-requests/');
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER,
            array("X-Api-Key:39ae74945179d56e3",
                  "X-Auth-Token:21f3446b20aab6"));
$payload = Array(
    'purpose' => 'ad',
    'amount' => $amt,
    'phone' => $payer_no,
    'buyer_name' => $payer_name,
    'redirect_url' => 'http://www.example.com/redirect/',
    'send_email' => true,
    'webhook' => 'http://www.example.com/webhook/',
    'send_sms' => true,
    'email' => 'foo@example.com',
    'allow_repeated_payments' => false
);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($payload));
$response = curl_exec($ch);
curl_close($ch);
echo $response;
?>

1 个答案:

答案 0 :(得分:3)

首先,将其解码为数组。然后访问它。

$result = json_decode($response,true);

echo $result['payment_request']['longurl'];