首页 > 语言 > PHP > 正文

常用的curl发包函数

2024-09-04 11:49:07
字体:
来源:转载
供稿:网友
  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();   //Vevb.com 
  33.             }   
  34.         }   
  35.         curl_close($ch);   
  36.         return $reponse;   
  37.     }

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表