array); default: throw new Exception('Unknown type: ' . $clz); } } public static function Object2XML($o) { $clz = get_class($o); $res = sprintf('<%s>', strtolower($clz)); switch($clz) { // simple data classes case 'T1': foreach(get_object_vars($o) as $nam => $val) { $res .= sprintf('<%s>%s', $nam, $val, $nam); } break; // array wrapper classes case 'T1Array': foreach($o->array as $elm) { $res .= Util::Object2XML($elm); } break; default: throw new Exception('Unknown type: ' . $clz); } $res .= sprintf('', strtolower($clz)); return $res; } public static function Object2String($o, $typ) { if($typ == 'application/xml') { return Util::Object2XML($o); } else if($typ == 'application/json') { return Util::Object2JSON($o); } else { throw new Exception('Unknown type: ' . $typ); } } public static function XML2Object($clz, $xml) { $temp = simplexml_load_string($xml); $res = new $clz(); switch($clz) { // simple data classes case 'T1': foreach(get_object_vars($res) as $nam => $val) { if(is_int($res->$nam)) { $res->$nam = (int)$temp->$nam; } else { $res->$nam = (string)$temp->$nam; } } break; // array wrapper classes case 'T1Array': $elmclz = substr($clz, 0, -5); foreach($temp as $elm) { $o = new $elmclz(); foreach(get_object_vars($o) as $nam => $val) { if(is_int($o->$nam)) { $o->$nam = (int)$elm->$nam; } else { $o->$nam = (string)$elm->$nam; } } $res->array[] = $o; } break; default: throw new Exception('Unknown type: ' . $clz); } return $res; } public static function JSON2Object($clz, $json) { $temp = json_decode($json); $res = new $clz(); switch($clz) { // simple data classes case 'T1': foreach(get_object_vars($res) as $nam => $val) { $res->$nam = $temp->$nam; } break; // array wrapper classes case 'T1Array': $elmclz = substr($clz, 0, -5); foreach($temp as $elm) { $o = new $elmclz(); foreach(get_object_vars($o) as $nam => $val) { $o->$nam = $elm->$nam; } $res->array[] = $o; } break; default: throw new Exception('Unknown type: ' . $clz); } return $res; } public static function String2Object($clz, $s, $typ) { if($typ == 'application/xml') { return Util::XML2Object($clz, $s); } else if($typ == 'application/json') { return Util::JSON2Object($clz, $s); } else { throw new Exception('Unknown type: ' . $typ); } } } class T1 { public $f1; public $f2; public function __construct($f1 = 0, $f2 = '') { $this->f1 = $f1; $this->f2 = $f2; } public function __toString() { return sprintf('{%s,%s}', $this->f1, $this->f2); } } class T1Array { public $array; public function __construct($array = array()) { $this->array = $array; } } include('/DivNative/32bit/php-5.6.23-Win32-VC11-x86/vendor/rmccue/requests/library/Requests.php'); Requests::register_autoloader(); function testGetOne($urlprefix, $f1, $acctyp) { $response = Requests::get($urlprefix . '/t1/' . $f1, array('Accept' => $acctyp)); echo (200 <= $response->status_code && $response->status_code < 300 ? 'true' : 'false') . "\r\n"; echo Util::String2Object('T1', $response->body, $acctyp) . "\r\n"; } function testGetAll($urlprefix, $acctyp) { $response = Requests::get($urlprefix . '/t1', array('Accept' => $acctyp)); echo (200 <= $response->status_code && $response->status_code < 300 ? 'true' : 'false') . "\r\n"; $a = Util::String2Object('T1Array', $response->body, $acctyp)->array; foreach($a as $o) echo $o; echo "\r\n"; } function testGetSome($urlprefix, $start, $finish, $acctyp) { $response = Requests::get($urlprefix . '/t1?start=' . $start . '&finish=' . $finish, array('Accept' => $acctyp)); echo (200 <= $response->status_code && $response->status_code < 300 ? 'true' : 'false') . "\r\n"; $a = Util::String2Object('T1Array', $response->body, $acctyp)->array; foreach($a as $o) echo $o; echo "\r\n"; } function testPost($urlprefix, $contyp, $o, $acctyp) { $response = Requests::post($urlprefix . '/t1', array('Accept' => $acctyp, 'Content-Type' => $contyp), Util::Object2String($o, $contyp)); echo (200 <= $response->status_code && $response->status_code < 300 ? 'true' : 'false') . "\r\n"; echo Util::String2Object('T1', $response->body, $acctyp) . "\r\n"; } function testPut($urlprefix, $contyp, $o) { $response = Requests::put($urlprefix . '/t1/' . $o->f1, array('Content-Type' => $contyp), Util::Object2String($o, $contyp)); echo (200 <= $response->status_code && $response->status_code < 300 ? 'true' : 'false') . "\r\n"; } function testDelete($urlprefix, $f1) { $response = Requests::delete($urlprefix . '/t1/' . $f1, array()); echo (200 <= $response->status_code && $response->status_code < 300 ? 'true' : 'false') . "\r\n"; } function test2($urlprefix, $typ) { echo '**** ' . $urlprefix . ' (' . $typ . ') ****' . "\r\n"; testGetOne($urlprefix, 123, $typ); testGetAll($urlprefix, $typ); testGetSome($urlprefix, 10, 12, $typ); testPost($urlprefix, $typ, new T1(123, 'ABC'), $typ); testPut($urlprefix, $typ, new T1(123, 'ABC')); testDelete($urlprefix, 123); } function test($urlprefix) { test2($urlprefix, "application/xml"); test2($urlprefix, "application/json"); } test("http://localhost:8080/wsrest/testapi1"); test("http://localhost:8080/wsrest/testapi2"); test("http://localhost:8080/wsrest/testapi3"); test("http://localhost/testapi.ashx"); test("http://localhost/testapi.svc"); test("http://localhost/testapi"); test("http://localhost:81/testapi.php"); ?>