How to convert JSON string data into array using PHP? -
i need convert json
result array in php. here using authorize.net
payment gateway sandbox. can response($result
) in json
string. not convert php array using json_decode
$output = json_decode($result,true); print_r($output);
sample code
<?php $data=array("createtransactionrequest" => array( "merchantauthentication" => array( "name" => "2bu77dwm", "transactionkey" => "92x86d7m7f6nhk98" ), "refid" => "9898989898", "transactionrequest" => array( "transactiontype" => "authcapturetransaction", "amount" => "25", "payment" => array( "creditcard" => array( "cardnumber" => "5424000000000015", "expirationdate" => "1220", "cardcode" => "999" ) ) ) ) ); $data_string = json_encode($data); $ch = curl_init('https://apitest.authorize.net/xml/v1/request.api'); curl_setopt($ch, curlopt_customrequest, "post"); curl_setopt($ch, curlopt_postfields, $data_string); curl_setopt($ch, curlopt_returntransfer, true); curl_setopt($ch, curlopt_httpheader, array( 'content-type: application/json', 'content-length: ' . strlen($data_string)) ); curl_setopt($ch, curlopt_ssl_verifypeer, false); $result = curl_exec($ch); curl_close($ch); print_r($result); //print json data //try convert json array $output = json_decode($result,true); print_r($output); //print empty
json response print_r($result);
http://www.jsoneditoronline.org/?id=c75c5a6a4c247ad2f0aaf7c801daad39
try below code result.
<?php $data=array("createtransactionrequest" => array( "merchantauthentication" => array( "name" => "2bu77dwm", "transactionkey" => "92x86d7m7f6nhk98" ), "refid" => "9898989898", "transactionrequest" => array( "transactiontype" => "authcapturetransaction", "amount" => "25", "payment" => array( "creditcard" => array( "cardnumber" => "5424000000000015", "expirationdate" => "1220", "cardcode" => "999" ) ) ) ) ); $data_string = json_encode($data); $ch = curl_init('https://apitest.authorize.net/xml/v1/request.api'); curl_setopt($ch, curlopt_customrequest, "post"); curl_setopt($ch, curlopt_postfields, $data_string); curl_setopt($ch, curlopt_returntransfer, true); curl_setopt($ch, curlopt_httpheader, array( 'content-type: application/json', 'content-length: ' . strlen($data_string)) ); curl_setopt($ch, curlopt_ssl_verifypeer, false); $result = curl_exec($ch); curl_close($ch); // below code $final_result = json_decode( preg_replace('/[\x00-\x1f\x80-\xff]/', '', $result), true ); echo "<pre>"; print_r($final_result); ?>
you need use
$output = json_decode( preg_replace('/[\x00-\x1f\x80-\xff]/', '', $result), true );print_r($output);
i have checked it! working me. hope help!
Comments
Post a Comment