+++ /dev/null
-<?php\r
-//require_once("restLibrary_curl.php");\r
-\r
-//A)\r
-//class doing the HTTP POST connection via cURL.\r
-//3rd and 4th parameters of the contructor are optional (define them only if here is a proxy between your server and the REST services)\r
-\r
- /**\r
- * backbone of the code based on "A sample class to read HTTP headers"\r
- * @author Geoffray Warnants - http://www.geoffray.be \r
- *(lines added by F Theeten for POST and Proxy connection)\r
- */\r
-\r
- class HTTPReaderCURLEDIT {\r
- protected $_url = null;\r
- protected $_headers = array();\r
- protected $_body = '';\r
-\r
- public function __construct($url, $fields, $proxy_url='', $proxy_login='') \r
- {\r
- \r
- $this->_url = curl_init($url);\r
- curl_setopt($this->_url, CURLOPT_RETURNTRANSFER, true);\r
- if(strlen($proxy_url)>0)\r
- {\r
- curl_setopt($this->_url, CURLOPT_PROXY, $proxy_url);\r
- }\r
- if(strlen($proxy_login)>0)\r
- { \r
- curl_setopt($this->_url, CURLOPT_PROXYUSERPWD, $proxy_login);\r
- }\r
- curl_setopt($this->_url, CURLOPT_HEADER, true);\r
- curl_setopt($this->_url, CURLOPT_POST, true);\r
- curl_setopt($this->_url, CURLOPT_POSTFIELDS, $fields);\r
- curl_setopt($this->_url, CURLOPT_HEADERFUNCTION, array($this, 'readHeaders'));\r
-\r
- }\r
- \r
- \r
- public function __destruct() {\r
- curl_close($this->_url);\r
- }\r
- \r
- // this must be called from outside function to do the connection\r
- public function getHeaders() {\r
- $this->_body = curl_exec($this->_url);\r
- return $this->_headers;\r
- }\r
- \r
- public function getBody() {\r
- return $this->_body;\r
- }\r
- \r
- protected function readHeaders($url, $str) {\r
- if (strlen($str) > 0) {\r
- $this->_headers[] = $str;\r
- }\r
-\r
- return strlen($str);\r
- }\r
- }\r
-\r
-//B\r
-//function doing the connection and returning the result\r
-function rest_helper($url, $params = null, $verb = 'GET', $format = 'json', $headerPOST='Content-type: application/x-www-form-urlencoded')\r
-{\r
-\r
- //mention your proxy parameters there in 3rd and 4th position (if needed) \r
- $myObj=new HTTPReaderCURLEDIT($url,$params);\r
- $myObj->getHeaders();\r
- $content = $myObj->getBody();\r
- $arrayContent=explode("\r\n\r\n", $content);\r
- \r
- if(count($arrayContent)>=2)\r
- {\r
- $res=$arrayContent[2];\r
- \r
- switch ($format) {\r
- case 'json':\r
- \r
- $r = json_decode($res);\r
- if ($r === null) {\r
- throw new Exception("failed to decode $res as json");\r
- }\r
- \r
- return $r;\r
- \r
- case 'xml':\r
- $r = simplexml_load_string($res);\r
- if ($r === null) {\r
- throw new Exception("failed to decode $res as xml");\r
- }\r
- return $r;\r
- \r
- case 'raw':\r
- return $res;\r
- case 'jsontoarray':\r
- return json_decode($res);\r
- }\r
- return $res;\r
- \r
- } \r
-}\r
-\r
-?>
\ No newline at end of file