curlChajian.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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($ishttps==1){
  72. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  73. }
  74. //设置head
  75. if($headarr){
  76. $heads = array();
  77. foreach($headarr as $k=>$v)$heads[] = ''.$k.':'.$v.'';
  78. curl_setopt($ch, CURLOPT_HTTPHEADER, $heads);
  79. }
  80. curl_setopt($ch, CURLOPT_TIMEOUT, $this->TIMEOUT);
  81. $output = curl_exec($ch);
  82. $this->setResponseHeaders($ch);
  83. curl_close($ch);
  84. return $output;
  85. }
  86. public function postcurl($url, $data=array(), $lx=0, $headarr=array())
  87. {
  88. if(!function_exists('curl_init')){
  89. return $this->postfilecont($url, $data);
  90. }
  91. $url = $this->strurl($url);
  92. $cont = $data;
  93. if($lx==0)$cont = $this->getdatastr($data);
  94. $ishttps = 0;
  95. if(substr($url,0, 5)=='https')$ishttps=1;
  96. $ch = curl_init();
  97. curl_setopt($ch, CURLOPT_URL, $url);
  98. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //要求结果为字符串且输出到屏幕上
  99. curl_setopt($ch, CURLOPT_HEADER, 0); //不返回header
  100. curl_setopt($ch, CURLOPT_POST, 1);
  101. @curl_setopt($ch, CURLOPT_POSTFIELDS, $cont);
  102. if($ishttps==1){
  103. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  104. //curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  105. }
  106. //设置head
  107. if($headarr){
  108. $heads = array();
  109. foreach($headarr as $k=>$v)$heads[] = ''.$k.':'.$v.'';
  110. curl_setopt($ch, CURLOPT_HTTPHEADER, $heads);
  111. }
  112. curl_setopt($ch, CURLOPT_TIMEOUT, $this->TIMEOUT);
  113. $output = curl_exec($ch);
  114. $curl_errno = curl_errno($ch);
  115. $this->setResponseHeaders($ch);
  116. curl_close($ch);
  117. return $output;
  118. }
  119. /**
  120. * postjson的类型
  121. */
  122. public function postjson($url, $data=array())
  123. {
  124. return $this->postcurl($url, $data, 0, array(
  125. 'Content-Type' => 'application/json'
  126. ));
  127. }
  128. public function getResponseHeaders()
  129. {
  130. return $this->ResponseHeaders;
  131. }
  132. private function setResponseHeaders($ch)
  133. {
  134. $this->ResponseHeaders = curl_getinfo($ch);
  135. }
  136. }
粤ICP备19079148号