curlChajian.php 3.7 KB

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