Aditya Aditya - 4 months ago 22
JSON Question

Pick a specific part from json string

I'm trying to connect my checkout page with payment gateway. For this I'm using php Curl and generating response. the response contains various details of payment but I need to pick up only url which is stored in "longurl" for redirection.


{ "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;
?>

Answer

First, decode it to array. then access it.

$result = json_decode($response,true);

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