1

I am trying to use the Transendit.com service with Codeigniter. Currently I am trying to build the notify page. I can receive the POST request (in JSON format) and write it to a file. The strange thing is that I cannot parse the JSON object to a PHP array so that I can extract relevant data from it. When I decode it before writing to the file the file is empty. If I don´t the JSON code is written to the file.

This is my controller code:

$result = $_POST['transloadit'];

$result = json_decode($result); // This produces empty content in file

$this->load->helper('file');

if ( ! write_file('./files/myfile.php', $result))
{
 echo 'Unable to write the file';
} else {
 echo 'File written!';
}

The JSON object that is sent to the page can be found here: http://pastie.org/3056727

2
  • It's only a json object until you decode it, then it's a PHP stdclass object. Commented Dec 22, 2011 at 11:13
  • My main objective is to extract data from this JSON object and write it to the database. How can I do that? (writing to the file is just so that I can see the output). Commented Dec 22, 2011 at 11:20

1 Answer 1

3

You're trying to write a stdclass object (the decoded json) directly to a file - that won't work.

Don't decode $result - use the original json string to write to the file. Also, .json is a valid file format - consider using it instead of .php (might make more sense).

$result = $_POST['transloadit'];
$this->load->helper('file');

// You can test for valid json like this:
$is_valid_json = json_decode($result) !== NULL;

if ( ! write_file('./files/myfile.json', $result)) {
    echo 'Unable to write the file';
}
else {
    echo 'File written!';
}
Sign up to request clarification or add additional context in comments.

3 Comments

Ah ok. But the reason I write it to a file is just to be able to "see" the result. The page I am working on is "hidden" page that does not display any information. It will update the database with the information from the JSON object. My main goal is to extract relevant data from the JSON object. How can I do that? Thanks!
You access it like any other object. Let's say $decoded = json_decode($result). So $decoded->message should return "The assembly was successfully completed." Since you actually have more than just key/value pairs here, you'll need to figure out how you want to store it in your database. I can't make that decision for you nor do I have the necessary information to do so.
I'm not sure why it's not working for you. Turn error reporting on full blast and use basic debugging techniques like var_dump(). Works fine for me here: codepad.org/V9ygBWgQ

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.