常用的curl发包函数

六月 08, 2019 | views
Comments 0

  1. function curl($url$postFields = null)   
  2.     {   
  3.         $ch = curl_init();   
  4.         curl_setopt($ch, CURLOPT_URL, $url);   
  5.         curl_setopt($ch, CURLOPT_FAILONERROR, false);   
  6.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);   
  7.    
  8.         if (is_array($postFields) && 0 < count($postFields))   
  9.         {   
  10.             $postBodyString = "";   
  11.             foreach ($postFields as $k => $v)   
  12.             {   
  13.                 $postBodyString .= "$k=" . urlencode($v) . "&";    
  14.             }   
  15.             unset($k$v);   
  16.             curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);     
  17.             curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);    
  18.             curl_setopt($ch, CURLOPT_POST, true);   
  19.             curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postBodyString,0,-1));   
  20.         }   
  21.         $reponse = curl_exec($ch);   
  22.         if (curl_errno($ch)){   
  23.             //throw new Exception(curl_error($ch),0);   
  24.             ShowMsg("授权出错,请联系管理员","/");   
  25.             exit();   
  26.         }   
  27.         else{   
  28.             $httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);   
  29.             if (200 !== $httpStatusCode){   
  30.                 //throw new Exception($reponse,$httpStatusCode);   
  31.                 ShowMsg("授权出错,请联系管理员","/");   
  32.                 exit();   //phpfensi.com 
  33.             }   
  34.         }   
  35.         curl_close($ch);   
  36.         return $reponse;   
  37.     }



zend