1

I need convert JSON result to array in PHP. Here I am using the authorize.net payment gateway sandbox. I can get the response($result) in JSON string. But I will not convert to php array by using the 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 the JSON data

    //Try to convert JSON into array
    $output = json_decode($result,true); 
    print_r($output); //Print empty

JSON response from print_r($result);

http://www.jsoneditoronline.org/?id=c75c5a6a4c247ad2f0aaf7c801daad39

7
  • can you share the sample json result? Commented Nov 8, 2016 at 10:23
  • Well, how should we help if you don't give us the content of the reply, so of that json formatted string? Commented Nov 8, 2016 at 10:23
  • if json_decode() cannot decode the string, then it is not valid JSON. Commented Nov 8, 2016 at 10:25
  • Please run the sample code and see the JSON resonse. Commented Nov 8, 2016 at 10:27
  • What error you are getting ? Commented Nov 8, 2016 at 10:28

1 Answer 1

2

Try below code you will get 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 is my code 
$final_result = json_decode( preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $result), true );
echo "<pre>";
print_r($final_result);

?>

you just need to use
$output = json_decode( preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $result), true );

print_r($output);

i have checked it! its working for me. Hope this will help!

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

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.