Curl.php 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: guanxl
  5. * Date: 2018/5/24
  6. * Time: 11:30
  7. */
  8. namespace App\Common;
  9. class Curl {
  10. private $http_code_ = 0;
  11. private $errno_ = 0;
  12. private $error_ = '';
  13. private $response_ = '';
  14. private $headers_ = array();
  15. private $options_ = array();
  16. private $info_ = array();
  17. private $handle_ = null;
  18. /**
  19. * CuteCurl constructor.
  20. * @param $url
  21. * @param bool|false $require_response_body 非200状态保存返回内容
  22. */
  23. function __construct($url, $require_response_body = false) {
  24. $this->handle_ = curl_init($url);
  25. $this->option(CURLOPT_FAILONERROR, !$require_response_body);
  26. }
  27. public function setTimeout($t) {
  28. $this->option('timeout', $t);
  29. }
  30. /**
  31. * @return int
  32. */
  33. public function getErrno() {
  34. return $this->errno_;
  35. }
  36. /**
  37. * @return string
  38. */
  39. public function getError() {
  40. return $this->error_;
  41. }
  42. /**
  43. * @return array
  44. */
  45. public function getInfo() {
  46. return $this->info_;
  47. }
  48. /**
  49. * @return int
  50. */
  51. public function getHttpCode() {
  52. return $this->http_code_;
  53. }
  54. public function httpGet() {
  55. return $this->send();
  56. }
  57. public function httpPost($params = array(), $options = array()) {
  58. // If its an array (instead of a query string) then format it correctly
  59. if (is_array($params)) {
  60. $params = http_build_query($params);
  61. }
  62. // Add in the specific options provided
  63. $this->options($options);
  64. $this->option(CURLOPT_POST, TRUE);
  65. $this->option(CURLOPT_POSTFIELDS, $params);
  66. return $this->send();
  67. }
  68. public function httpHeader($header, $content = NULL) {
  69. $this->headers_[] = $content ? $header . ': ' . $content : $header;
  70. }
  71. public function sendCookie($cookie,$prefix) {
  72. if(!empty($cookie)) {
  73. $str = '';
  74. foreach ($cookie as $key => $value) {
  75. if(strpos($key,$prefix) === 0 ) {
  76. $str .= $key.'='.$value.';';
  77. }
  78. }
  79. $this->headers_[] = 'Cookie: '.substr($str,0,-1);
  80. }
  81. }
  82. private function send() {
  83. // Set two default options_, and merge any extra ones in
  84. if (!isset($this->options_[CURLOPT_TIMEOUT])) {
  85. $this->options_[CURLOPT_TIMEOUT] = 30;
  86. }
  87. if (!isset($this->options_[CURLOPT_RETURNTRANSFER])) {
  88. $this->options_[CURLOPT_RETURNTRANSFER] = TRUE;
  89. }
  90. if (!isset($this->options_[CURLOPT_FAILONERROR])) {
  91. $this->options_[CURLOPT_FAILONERROR] = TRUE;
  92. }
  93. if (!empty($this->headers_)) {
  94. $this->option(CURLOPT_HTTPHEADER, $this->headers_);
  95. }
  96. $this->options();
  97. // Execute the request & and hide all output
  98. $this->response_ = curl_exec($this->handle_);
  99. $this->info_ = curl_getinfo($this->handle_);
  100. $this->http_code_ = $this->info_['http_code'];
  101. // Request failed
  102. if ($this->response_ === FALSE) {
  103. $this->errno_ = curl_errno($this->handle_);
  104. $this->error_ = curl_error($this->handle_);
  105. curl_close($this->handle_);
  106. return FALSE;
  107. } // Request successful
  108. else {
  109. curl_close($this->handle_);
  110. return $this->response_;
  111. }
  112. }
  113. public function ssl($verify_peer = TRUE, $verify_host = 2, $path_to_cert = NULL) {
  114. if ($verify_peer) {
  115. $this->option(CURLOPT_SSL_VERIFYPEER, TRUE);
  116. $this->option(CURLOPT_SSL_VERIFYHOST, $verify_host);
  117. $this->option(CURLOPT_CAINFO, $path_to_cert);
  118. } else {
  119. $this->option(CURLOPT_SSL_VERIFYPEER, FALSE);
  120. }
  121. return $this;
  122. }
  123. public function options($options = array()) {
  124. // Merge options in with the rest - done as array_merge() does not overwrite numeric keys
  125. foreach ($options as $option_code => $option_value) {
  126. $this->option($option_code, $option_value);
  127. }
  128. // Set all options provided
  129. curl_setopt_array($this->handle_, $this->options_);
  130. return $this;
  131. }
  132. public function option($code, $value) {
  133. if (is_string($code) && !is_numeric($code)) {
  134. $code = constant('CURLOPT_' . strtoupper($code));
  135. }
  136. $this->options_[$code] = $value;
  137. return $this;
  138. }
  139. }