+++ /dev/null
-<?php\r
-\r
-function do_post_request($url, $data, $optional_headers = null)\r
-{\r
- $params = array('http' => array(\r
- 'method' => 'POST',\r
- 'content' => $data\r
- ));\r
- if ($optional_headers !== null) {\r
- $params['http']['header'] = $optional_headers;\r
- }\r
- $ctx = stream_context_create($params);\r
- $fp = @fopen($url, 'rb', false, $ctx);\r
- if (!$fp) {\r
- throw new Exception("Problem with $url, $php_errormsg");\r
- }\r
- $response = @stream_get_contents($fp);\r
- if ($response === false) {\r
- throw new Exception("Problem reading data from $url, $php_errormsg");\r
- }\r
- return $response;\r
-}\r
-\r
-\r
-function rest_helper($url, $params = null, $verb = 'GET', $format = 'json', $headerPOST='Content-type: application/x-www-form-urlencoded')\r
-{\r
- $cparams = array(\r
- 'http' => array(\r
- 'method' => $verb,\r
- 'ignore_errors' => true,\r
- )\r
- );\r
- if ($params !== null) {\r
- $params = http_build_query($params);\r
- if ($verb == 'POST') {\r
- $cparams['http']['header'] = $headerPOST;\r
- $cparams['http']['content'] = $params;\r
- } else {\r
- $url .= '?' . $params;\r
- }\r
- }\r
-\r
- $context = stream_context_create($cparams);\r
- $fp = fopen($url, 'rb', false, $context);\r
- if (!$fp) {\r
- $res = false;\r
- } else {\r
- // If you're trying to troubleshoot problems, try uncommenting the\r
- // next two lines; it will show you the HTTP response headers across\r
- // all the redirects:\r
- // $meta = stream_get_meta_data($fp);\r
- // var_dump($meta['wrapper_data']);\r
- $res = stream_get_contents($fp);\r
- //print(fgets($fp));\r
- }\r
-\r
- if ($res === false) {\r
- throw new Exception("$verb $url failed: $php_errormsg");\r
- }\r
-\r
- switch ($format) {\r
- case 'json':\r
- //print('json'); \r
- //print($res);\r
- $r = json_decode($res);\r
- if ($r === null) {\r
- throw new Exception("failed to decode $res as json");\r
- }\r
- //print('display json');\r
- //print_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
- //print_r($res);\r
- return json_decode($res);\r
- }\r
- return $res;\r
-}\r
-\r
-\r
-\r
-?>
\ No newline at end of file