1

I've done this many times before with various SAAS services, but I can't parse the supposedly JSON responses I'm getting from Blitline image processing's API.

Here's what I do to handle the POST:

$body=@file_get_contents('php://input');

print_r($body); 

results=%7B%22original_meta%22%3A%7B%22width

OR

$body=rawurldecode($body);

print_r($body); 

results={"original_meta":{"width ...

When I go to print $body->original_meta->width, I get an empty string. You'll realize I didn't json_decode() the $body but that's because that returns an empty string too.

Removing the results= with substr($body, 8) doesn't help either.

Can anyone help?

6
  • 1
    Why don't you start by removing the error suppression and see if that does anything Commented Apr 16, 2014 at 19:51
  • I don't understand your code blocks...is "results=" part of your code? Or is that the response? If the latter, is "results=" part of the response, or is that just your way of showing us what the code resulted in? Commented Apr 16, 2014 at 19:54
  • 1
    It looks like you are perhaps getting not getting a JSON string POSTed to the script, but rather a query string with parameter results with a URL-encoded JSON string value. Perhaps in this case you should actually be getting your value from $_POST (that is of course if request is coming in with form-encoded content type header. Commented Apr 16, 2014 at 19:55
  • 2
    Why are you dealing with the raw post? Seems you just want $data = json_decode($_POST['results']). Commented Apr 16, 2014 at 19:57
  • I was just going to say it would make sense to deal with $_POST instead of reading input. Commented Apr 16, 2014 at 19:58

2 Answers 2

2

Expanding on my comment: the POST data is standard x-www-form-urlencoded data so there's no need to access the raw POST data. You can simply access the $_POST array that contains the URL decoded data:

$data = json_decode($_POST['results']);
echo $data->original_meta->width;
Sign up to request clarification or add additional context in comments.

Comments

1

Ok this is pretty ugly but it works...

$body = file_get_contents('php://input');

$body=rawurldecode($body);

$body=substr($body, 8); 

$body=json_decode($body);

echo $body->original_meta->width; //1936

1 Comment

I think another option would be to use parse_str($body, $data); json_decode($data['results']);.

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.