0

Below is my code, I am trying to get particular api response (msg,amt) in php string. ........................................................................................................................................

$key = "XXXXX";
$mykey = "XXXXX";
$command = "Check";
$value = "5454355435"; 


$r = array('key' => $key , 'value' => $value, 'command' =>  $command);

$qs= http_build_query($r);
$wsUrl = "https://info.service-provider.com";

$c = curl_init();
curl_setopt($c, CURLOPT_URL, $wsUrl);
curl_setopt($c, CURLOPT_POST, 1);
curl_setopt($c, CURLOPT_POSTFIELDS, $qs);
curl_setopt($c, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, 0);
$o = curl_exec($c);
if (curl_errno($c)) {
  $sad = curl_error($c);
  throw new Exception($sad);
}
curl_close($c);

$valueSerialized = @unserialize($o);
if($o === 'b:0;' || $valueSerialized !== false) {
  print_r($valueSerialized);
}

print_r($o);

RESPONSE:

{"status":1,"msg":"1 out of 1 Transactions Fetched Successfully","transaction_details":{"2767712494": {"mihpayid":"268999084","request_id":"","ref_num":"020814301298","amt":"1.00","txnid":"5454355435","additional_charges":"0.00","productinfo":"SHIRT"}}}
2
  • You need to decode the json string use print_r(json_decode($valueSerialized,true)); Commented Mar 13, 2015 at 5:31
  • what value you need? Commented Mar 13, 2015 at 5:34

2 Answers 2

2

Your string is in json format. To get value from it, you should convert it into an array like this:

$json = '{"status":1,"msg":"1 out of 1 Transactions Fetched Successfully","transaction_details":{"2767712494": {"mihpayid":"268999084","request_id":"","ref_num":"020814301298","amt":"1.00","txnid":"5454355435","additional_charges":"0.00","productinfo":"SHIRT"}}}';

$array = json_decode($json, true);

echo '<pre>'; print_r($array);

Your array will look like this:

Array
(
    [status] => 1
    [msg] => 1 out of 1 Transactions Fetched Successfully
    [transaction_details] => Array
        (
            [2767712494] => Array
                (
                    [mihpayid] => 268999084
                    [request_id] => 
                    [ref_num] => 020814301298
                    [amt] => 1.00
                    [txnid] => 5454355435
                    [additional_charges] => 0.00
                    [productinfo] => SHIRT
                )

        )

)

To get msg you should write like this:

echo $array['msg'];

You can get more information from json_decode

Let me know for more help.

Sign up to request clarification or add additional context in comments.

1 Comment

Actually, I was trying the same code but just below to print_r($o); .So, because of If statement, I was getting error. Thnx.
0

This response looks like JSON format. Pass this response string to php method json_decode like:
$response = json_decode($yourResponseString,true);
and then you should be able to access it's properties like a regular associative array:
$msg = $response['msg'];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.