Refactored php methods to send and receive json, and to handle more general input and output. Also improved error reporting and cleaned up code