×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: PHP
Posted by: Nilesh Yadav
Added: Jun 7, 2017 12:37 PM
Views: 2337
Tags: no tags
  1. /**
  2.  * Generic REST PHP client (Tested)
  3.  * @see $this->rest_call('https://httpbin.org', '/post', 'POST', array('test' => date('Y-m-d H:i:s')), array(CURLAUTH_DIGEST, 'user', 'pass'), true);
  4.  * @param string $path
  5.  * @param string $method
  6.  * @param array $vars
  7.  * @param array $auth
  8.  * @param boolean $debug
  9.  * @return boolean || string
  10.  */
  11. function rest_call($url, $path = '', $method = 'GET', $vars = array(), $auth = array(), $debug = false) {
  12.     defined('ERROR_LOG') or define('ERROR_LOG', tempnam(sys_get_temp_dir(),'REST'));
  13.     defined('PHP_VERSION_ID') or define('PHP_VERSION_ID', preg_replace('/[^0-9A-Za-z\.]/', '', PHP_VERSION));
  14.  
  15.     if (empty($url)) {
  16.         file_put_contents(ERROR_LOG, "REST call url not set\n", FILE_APPEND);
  17.         return false;
  18.     }
  19.  
  20.     $url .= $path;
  21.     $tmpfile = '';
  22.     $postdata = http_build_query($vars, '', '&');
  23.     if ($method == 'GET' && count($vars) > 0) {
  24.         $url .= '?' . $postdata;
  25.     }
  26.  
  27.     $ch = curl_init($url);
  28.     if ($debug) {
  29.         curl_setopt($ch, CURLOPT_VERBOSE, 1); $fp = fopen(ERROR_LOG, 'w'); curl_setopt($ch, CURLOPT_STDERR, $fp);
  30.     }
  31.  
  32.     switch ($method) {
  33.         case 'GET':
  34.             curl_setopt($ch, CURLOPT_HTTPGET, true);
  35.             break;
  36.         case 'POST':
  37.             curl_setopt($ch, CURLOPT_POST, true);
  38.             curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
  39.             break;
  40.         case 'PUT':
  41.             curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
  42.             curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
  43.             file_put_contents($tmpfile = tempnam(sys_get_temp_dir(),'RESTPUT'), $postdata);
  44.             curl_setopt($ch, CURLOPT_INFILE, $fp = fopen($tmpfile, 'r'));
  45.             curl_setopt($ch, CURLOPT_INFILESIZE, filesize($tmpfile));
  46.             break;
  47.         case 'DELETE':
  48.             curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
  49.             curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
  50.             break;
  51.         default:
  52.             curl_setopt($ch, CURLOPT_HTTPGET, true);
  53.             break;
  54.     }
  55.  
  56.     if (isset($auth[0]) && isset($auth[1]) && isset($auth[2])) {
  57.         switch ($auth[0]) {
  58.             case 'basic':
  59.             case CURLAUTH_BASIC:
  60.                 curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  61.                 curl_setopt($ch, CURLOPT_USERPWD, $auth[1].':'.$auth[2]);
  62.                 break;
  63.             case 'digest':
  64.             case CURLAUTH_DIGEST:
  65.                 curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
  66.                 curl_setopt($ch, CURLOPT_USERPWD, $auth[1].':'.$auth[2]);
  67.                 break;
  68.             case 'none':
  69.             case CURLAUTH_NONE:
  70.             default:
  71.                 curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_NONE);
  72.                 break;
  73.         }
  74.     }
  75.  
  76.     curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; REST PHP caller; PHP/'.PHP_VERSION_ID.')');
  77.     curl_setopt($ch, CURLOPT_HEADER, 0);
  78.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  79.     curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
  80.     curl_setopt($ch, CURLOPT_TIMEOUT, 5);
  81.     $res = curl_exec($ch);
  82.     $errno = curl_errno($ch); $errmsg = curl_error($ch);
  83.     $http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  84.     curl_close($ch);
  85.  
  86.     if (strlen($tmpfile)) {
  87.         unlink($tmpfile);
  88.     }
  89.  
  90.     if ($debug) {
  91.         fclose($fp);
  92.     }
  93.  
  94.     if ($res === false || $errno != 0) {
  95.         file_put_contents(ERROR_LOG, "REST call error {$errmsg}\n", FILE_APPEND);
  96.         return false;
  97.     }
  98.  
  99.     if ($http_status != 200) {
  100.         file_put_contents(ERROR_LOG, "REST call http status {$http_status}\n", FILE_APPEND);
  101.         return false;
  102.     }
  103.  
  104.     return $res;
  105. }