Skip to content

Instantly share code, notes, and snippets.

@stuffmc
Created July 23, 2014 20:49
Show Gist options
  • Select an option

  • Save stuffmc/194753a33748e21dc761 to your computer and use it in GitHub Desktop.

Select an option

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

loicm commented Jul 23, 2014

Copy link
Copy Markdown

$log = json_decode($cont);

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

@PofMagicfingers

Copy link
Copy Markdown

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

loicm commented Jul 23, 2014

Copy link
Copy Markdown

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

stuffmc commented Jul 23, 2014

Copy link
Copy Markdown
Author

Thanks guys!!!

@PofMagicfingers

Copy link
Copy Markdown

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

@loicm

loicm commented Jul 23, 2014

Copy link
Copy Markdown

@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