Advertisement
ravel571

getInfo

Nov 9th, 2016
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.56 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4. * Simple GET request
  5. * getinfo("getRequest");
  6. *
  7. * Get request with headers
  8. * getinfo("getRequest", null, arrayHeaders);
  9. *
  10. * Post request
  11. * getinfo("url", "arrayPost[key => value] or arrayPost[value]");
  12. *
  13. * Post request with headers
  14. * getinfo("url", "arrayPost[key => value] or arrayPost[value]", arrayHeaders);
  15. */
  16.  
  17. function getInfo (...$data) {
  18.    
  19.     if (count($data) >= 1 AND count($data) <= 3) {
  20.        
  21.         $ch = curl_init();
  22.         curl_setopt($ch, CURLOPT_URL, $data[0]);
  23.         curl_setopt($ch, CURLOPT_FAILONERROR, true);
  24.        
  25.         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  26.         curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  27.  
  28.         curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);       
  29.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  30.         curl_setopt($ch, CURLOPT_TIMEOUT, 3);
  31.        
  32.         if (isset($data[1]) AND !empty($data[1])) {
  33.            
  34.             if (is_array($data[1])) {
  35.                
  36.                 $array = array();
  37.                
  38.                 foreach ($data[1] as $key => $val)
  39.                 {
  40.                     if (!is_int($key)) {
  41.                        
  42.                         $array[] = $key."=".$val;
  43.                        
  44.                     }
  45.                     else {
  46.                        
  47.                         $arrayNotKey[] = $val;
  48.                        
  49.                     }
  50.                
  51.                 }
  52.                
  53.                 if (isset($arrayNotKey) AND count($arrayNotKey) > 0) {
  54.                    
  55.                     $postdata = $arrayNotKey[0];
  56.                    
  57.                 }
  58.                 else {
  59.                    
  60.                     $postdata = implode("&", $array);
  61.                    
  62.                 }
  63.                
  64.             curl_setopt($ch, CURLOPT_POST, true);
  65.             curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
  66.                
  67.             }
  68.             else {
  69.                
  70.                 throw new Exception("2-ой аргумент функции ".__FUNCTION__." не является массивом.");
  71.                
  72.             }
  73.            
  74.         }
  75.        
  76.         if (isset($data[2]) AND !empty($data[2])) {
  77.            
  78.             curl_setopt($ch, CURLOPT_HTTPHEADER, $data[2]);
  79.            
  80.         }
  81.        
  82.         $data = curl_exec($ch);
  83.         $info = curl_getinfo($ch);
  84.         $info['data'] = $data;
  85.         curl_close($ch);
  86.        
  87.         return $info;
  88.        
  89.     }
  90.     else {
  91.        
  92.        throw new Exception("Не верное количество аргументов у функции ".__FUNCTION__.".");
  93.        
  94.     }
  95.    
  96. }
  97.  
  98. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement