0

I'm having a bit of a problem reading JSON data that I generate in PHP and then pass back to my Javascript and I'm not sure why.

Here is the PHP:

header("content-type: text/json");

curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);

//execute post
$result = curl_exec($ch);
// echo the result as a JSON object
echo json_encode($result);

And here is the Javscript:

$.post("payment_do.php", { "token": response.response.token, "ip_address": response.ip_address}).done(function(data) {
    console.log(data);
});

It seems as though if I take the header line away in the PHP I get a response that I can read in the Javascript but I cannot access any of the elements the way you would expect. If I leave the header in there, I get no response readable by the javascript.

EDIT: Now getting a response from the php, looks like this:

"{\"response\":{\"token\":\"ch_9knTXHoU0dVZsl7iMHyHGg\",\"success\":true,\"amount\":9900,\"currency\":\"AUD\",\"description\":\"test\",\"email\":\"[email protected]\",\"ip_address\":\"1.1.1.1\",\"created_at\":\"2013-03-18T23:49:12Z\",\"status_message\":\"Success!\",\"error_message\":null,\"card\":{\"token\":\"test_token\",\"display_number\":\"XXXX-XXXX-XXXX-0000\",\"scheme\":\"master\",\"address_line1\":\"123 Fake Street Fakington\",\"address_line2\":null,\"address_city\":\"moon\",\"address_postcode\":\"2121\",\"address_state\":\"NSW\",\"address_country\":\"Australia\"},\"transfer\":[],\"amount_refunded\":0,\"total_fees\":999,\"merchant_entitlement\":999,\"refund_pending\":false}}"

In the end I had to remove the json_encode function in the php and just return the result. In the javascript (using jQuery) I then called:

data = $.parseJSON(data);

With which I could then access the elements of the object.

2 Answers 2

6

Change your header to application/json (as opposed to text/json). In addition, if you actually want to further process the results of the curl_exec call then you need to set an additional option:

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

This will cause the curl_exec call to return data on success, as opposed to TRUE.

$result = curl_exec($ch);
if(! $result) {
    // handle error
}

echo json_encode($result);

Also, you will want to verify the data you are getting back from the cURL call - make sure that it really needs to be encoded before being returned.

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

7 Comments

Thanks, I've tried this. I still get the error where I can't see the JSON. I just get no response in the javascript, as soon as I comment this line out. I can see the JSON response but can't access any of the elements.
So, you see the JSON data come back as a response, in the console, but you can't use it? Is that a fair statement?
Yes - exactly. It seems that cURL is returning the data without me explicitly telling it to. The $result variable just true which is encoded as 1 by the json_encode.
@rheotron - good additional info. I've edited my answer to reflect some additional things you probably need to implement.
Yeah - that's working now. I can get the correct data back from the cURL. The only problem I'm having now is that cURL seems to put in a whole lot of slashes and it seems to be returned to the Javascript as a string even with the json_encode. Any ideas?
|
2

add this:

header("Content-Type: application/json");
echo json_encode($result);

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.