Consolidate duplicated PHP code, add support for specifying scheme and TLS
Clean up PHP code, begin adding TLS support