[关闭]
@52fhy 2015-11-14T12:20:40.000000Z 字数 1208 阅读 551

curl

curl


  1. /**
  2. * Make an HTTP request
  3. *
  4. * @return string API results
  5. * @ignore
  6. */
  7. private static function http($url, $method, $postfields = NULL, $headers = array()) {
  8. try{
  9. $ci = curl_init();
  10. /* Curl settings */
  11. curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
  12. curl_setopt($ci, CURLOPT_USERAGENT, 'KdtApiSdk Client v0.1');
  13. curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 30);
  14. curl_setopt($ci, CURLOPT_TIMEOUT, 30);
  15. curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
  16. curl_setopt($ci, CURLOPT_ENCODING, "");
  17. curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, false);
  18. curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, 2);
  19. //curl_setopt($ci, CURLOPT_HEADERFUNCTION, array($this, 'getHeader'));
  20. curl_setopt($ci, CURLOPT_HEADER, FALSE);
  21. switch ($method) {
  22. case 'POST':
  23. curl_setopt($ci, CURLOPT_POST, TRUE);
  24. if (!empty($postfields)) {
  25. curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
  26. }
  27. break;
  28. }
  29. curl_setopt($ci, CURLOPT_URL, $url );
  30. curl_setopt($ci, CURLOPT_HTTPHEADER, $headers );
  31. curl_setopt($ci, CURLINFO_HEADER_OUT, TRUE );
  32. $response = curl_exec($ci);
  33. $httpCode = curl_getinfo($ci, CURLINFO_HTTP_CODE);
  34. $httpInfo = curl_getinfo($ci);
  35. //获取错误信息
  36. if (FALSE === $response)
  37. throw new Exception(curl_error($ci), curl_errno($ci));
  38. } catch(Exception $e) {
  39. throw $e;
  40. }
  41. //echo '<pre>';
  42. //var_dump($response);
  43. //var_dump($httpInfo);
  44. curl_close ($ci);
  45. return $response;
  46. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注