curlChajian.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /**
  3. curl
  4. */
  5. class curlChajian extends Chajian{
  6. private $TIMEOUT = 30;
  7. private $ResponseHeaders;
  8. private function strurl($url)
  9. {
  10. $url = str_replace('&#47;', '/', $url);
  11. $url = str_replace(' ', '', $url);
  12. $url = str_replace("\n", '', $url);
  13. return $url;
  14. }
  15. /**
  16. * 设置超时是手机
  17. * $ms 秒数
  18. */
  19. public function setTimeout($ms)
  20. {
  21. $this->TIMEOUT = $ms;
  22. return $this;
  23. }
  24. private function getdatastr($data)
  25. {
  26. $cont = '';
  27. if(is_array($data)){
  28. foreach($data as $k=>$v)$cont.='&'.$k.'='.$v.'';
  29. if($cont!='')$cont=substr($cont,1);
  30. }else{
  31. $cont = $data;
  32. }
  33. return $cont;
  34. }
  35. public function getfilecont($url)
  36. {
  37. $url = $this->strurl($url);
  38. @$result = file_get_contents($url);
  39. return $result;
  40. }
  41. public function postfilecont($url, $data=array())
  42. {
  43. $url = $this->strurl($url);
  44. $cont = $this->getdatastr($data);
  45. $len = strlen($cont);
  46. $opts = array(
  47. 'http' => array(
  48. 'method' => 'POST',
  49. 'header' =>
  50. "Content-type: application/x-www-form-urlencoded\r\n" .
  51. "Content-length: $len\r\n",
  52. 'content' => $cont,
  53. )
  54. );
  55. $cxContext = stream_context_create($opts);
  56. @$sFile = file_get_contents($url, false, $cxContext);
  57. return $sFile;
  58. }
  59. public function getcurl($url, $headarr=array())
  60. {
  61. if(!function_exists('curl_init')){
  62. return $this->getfilecont($url);
  63. }
  64. $url= $this->strurl($url);
  65. $ishttps = 0;
  66. if(substr($url,0, 5)=='https')$ishttps=1;
  67. $ch = curl_init();
  68. curl_setopt($ch, CURLOPT_URL, $url);
  69. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  70. curl_setopt($ch, CURLOPT_HEADER, 0);
  71. if($this->rock->HTTPweb)curl_setopt($ch, CURLOPT_USERAGENT, $this->rock->HTTPweb);
  72. //if($this->rock->ip)$headarr['X-FORWARDED-FOR'] = $this->rock->ip;
  73. if($ishttps==1){
  74. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  75. }
  76. //设置head
  77. if($headarr){
  78. $heads = array();
  79. foreach($headarr as $k=>$v)$heads[] = ''.$k.':'.$v.'';
  80. curl_setopt($ch, CURLOPT_HTTPHEADER, $heads);
  81. }
  82. curl_setopt($ch, CURLOPT_TIMEOUT, $this->TIMEOUT);
  83. $output = curl_exec($ch);
  84. $this->setResponseHeaders($ch);
  85. curl_close($ch);
  86. return $output;
  87. }
  88. public function postcurl($url, $data=array(), $lx=0, $headarr=array())
  89. {
  90. if(!function_exists('curl_init')){
  91. return $this->postfilecont($url, $data);
  92. }
  93. $url = $this->strurl($url);
  94. $cont = $data;
  95. if($lx==0)$cont = $this->getdatastr($data);
  96. $ishttps = 0;
  97. if(substr($url,0, 5)=='https')$ishttps=1;
  98. $ch = curl_init();
  99. curl_setopt($ch, CURLOPT_URL, $url);
  100. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //要求结果为字符串且输出到屏幕上
  101. curl_setopt($ch, CURLOPT_HEADER, 0); //不返回header
  102. if($this->rock->HTTPweb)curl_setopt($ch, CURLOPT_USERAGENT, $this->rock->HTTPweb);
  103. curl_setopt($ch, CURLOPT_POST, 1);
  104. @curl_setopt($ch, CURLOPT_POSTFIELDS, $cont);
  105. if($ishttps==1){
  106. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  107. //curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  108. }
  109. //if($this->rock->ip)$headarr['X-FORWARDED-FOR'] = $this->rock->ip;
  110. //设置head
  111. if($headarr){
  112. $heads = array();
  113. foreach($headarr as $k=>$v)$heads[] = ''.$k.':'.$v.'';
  114. curl_setopt($ch, CURLOPT_HTTPHEADER, $heads);
  115. }
  116. curl_setopt($ch, CURLOPT_TIMEOUT, $this->TIMEOUT);
  117. $output = curl_exec($ch);
  118. $curl_errno = curl_errno($ch);
  119. $this->setResponseHeaders($ch);
  120. curl_close($ch);
  121. return $output;
  122. }
  123. /**
  124. * postjson的类型
  125. */
  126. public function postjson($url, $data=array())
  127. {
  128. return $this->postcurl($url, $data, 0, array(
  129. 'Content-Type' => 'application/json'
  130. ));
  131. }
  132. public function getResponseHeaders()
  133. {
  134. return $this->ResponseHeaders;
  135. }
  136. private function setResponseHeaders($ch)
  137. {
  138. $this->ResponseHeaders = curl_getinfo($ch);
  139. }
  140. }
粤ICP备19079148号