Skip to content

Instantly share code, notes, and snippets.

@stuffmc
Created July 23, 2014 20:49
Show Gist options
  • Save stuffmc/194753a33748e21dc761 to your computer and use it in GitHub Desktop.
Save stuffmc/194753a33748e21dc761 to your computer and use it in GitHub Desktop.
// curl -X POST -H "Content-Type: body=application/json" -d '{"logs":["xyz"]}' https://example.com/v1/log
$cont = file_get_contents('php://input');
echo "cont: $cont\n"; // Outputs cont: {"logs":["xyz"]}
$log = json_decode($cont);
echo "log: $log\n"; // Never outputed. Even not "log:"
@loicm
Copy link

loicm commented Jul 23, 2014

$log = json_decode($cont);

// then you can:
echo $log->logs[0];
// or
foreach($log->logs as $l) {
echo $l;
}

@PofMagicfingers
Copy link

Never outputed surely because of error level.

I get :
Catchable fatal error: Object of class stdClass could not be converted to string

Try :

error_reporting(E_ALL);

To get a debug string of an object use :

echo "log: ".var_dump($log)."\n";

@loicm
Copy link

loicm commented Jul 23, 2014

But indeed you'd rather check first if $log is not NULL because that's the return value of json_decode() in case in invalid json string.

@stuffmc
Copy link
Author

stuffmc commented Jul 23, 2014

Thanks guys!!!

@PofMagicfingers
Copy link

@loicm : That's not the issue here. Issue was getting $log to echo

@loicm
Copy link

loicm commented Jul 23, 2014

@PofMagicfingers this was because he wanted to access the object as a string as he didn't know how to deal with objects in php. But agree that it's error level didn't help him to debug as the error went silently…

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment