常用的curl发包函数

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