[小技巧] 【使用Curl组件GET/POST数据】

  1. //CurlGet数据
  2. function_curl_get($url){
  3. $ch=curl_init();
  4. curl_setopt($ch,CURLOPT_URL,$url);
  5. curl_setopt($ch,CURLOPT_HEADER,0);
  6. curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  7. $data=curl_exec($ch);
  8. curl_close($ch);
  9. if($data)
  10. return$data;
  11. else
  12. returnfalse;
  13. }
  14. //CurlPost数据
  15. function_curl_post($url,$vars){
  16. $ch=curl_init();
  17. curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  18. curl_setopt($ch,CURLOPT_URL,$url);
  19. curl_setopt($ch,CURLOPT_POST,1);
  20. curl_setopt($ch,CURLOPT_POSTFIELDS,$vars);
  21. $data=curl_exec($ch);
  22. curl_close($ch);
  23. if($data)
  24. return$data;
  25. else
  26. returnfalse;
  27. }

通用函数

  1. /**
  2. *使用GET方法发送HTTP请求
  3. *
  4. *@paramstring$url需要请求的URL,完整URL,例如:http://www.example.com:8080/test.php?parm1=var1&parm2=var2
  5. *@paramarray/string$cookies如果有COOKIE数据可以发送过去,可以是Cookie数组,也可以是Cookie字符串
  6. *@returnmixed成功返回GET回来的数据,失败返回false
  7. */
  8. functionhttp_get($url,$cookies=array()){
  9. /**
  10. *使用cURL处理GET请求
  11. */
  12. if(function_exists('curl_init')){
  13. //组织COOKIE数据
  14. $header=array();
  15. if(!emptyempty($cookies)){
  16. if(is_array($cookies)){
  17. $encoded='';
  18. while(list($k,$v)=each($cookies)){
  19. $encoded.=($encoded?";":"");
  20. $encoded.=rawurlencode($k)."=".rawurlencode($v);
  21. }
  22. $header=array("Cookie:".$encoded);
  23. }elseif(is_string($cookies)){
  24. if(strtolower(substr($cookies,0,7))=='cookie:'){
  25. $header=array($cookies);
  26. }else{
  27. $header=array("Cookie:".$cookies);
  28. }
  29. }
  30. }
  31. //处理请求
  32. $ch=curl_init();
  33. curl_setopt($ch,CURLOPT_URL,$url);
  34. curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
  35. curl_setopt($ch,CURLOPT_HEADER,0);
  36. curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  37. $data=curl_exec($ch);
  38. curl_close($ch);
  39. if($data)
  40. return$data;
  41. else
  42. returnfalse;
  43. }
  44. /**
  45. *使用fsockopen处理GET请求
  46. */
  47. else{
  48. //组织COOKIE数据
  49. $cookie='';
  50. if(!emptyempty($cookies)){
  51. if(is_array($cookies)){
  52. $encoded='';
  53. while(list($k,$v)=each($cookies)){
  54. $encoded.=($encoded?";":"");
  55. $encoded.=rawurlencode($k)."=".rawurlencode($v);
  56. }
  57. $cookie=$encoded;
  58. }elseif(is_string($cookies)){
  59. if(strtolower(substr($cookies,0,7))=='cookie:'){
  60. $cookie=substr($cookies,7);
  61. }else{
  62. $cookie=$cookies;
  63. }
  64. }
  65. }
  66. //处理请求
  67. $url=parse_url($url);
  68. if(strtolower($url['scheme'])!='http'&&$url['scheme']!=''){
  69. returnfalse;
  70. }
  71. if(!($fp=fsockopen($url['host'],$url['port']?$url['port']:80,$errno,$errstr,10))){
  72. returnfalse;
  73. }
  74. fputs($fp,sprintf("GET%s%s%sHTTP/1.0/n",$url['path'],$url['query']?"?":"",$url['query']));
  75. fputs($fp,"Host:$url[host]/n");
  76. fputs($fp,"User-Agent:HFHttp-Client/n");
  77. if($cookie!=''){
  78. fputs($fp,"Cookie:$cookie/n/n");
  79. }
  80. fputs($fp,"Connection:close/n/n");
  81. fputs($fp,"$post/n");
  82. $ret='';
  83. while(!feof($fp)){
  84. $c=fgets($fp,1024);
  85. if(preg_match("/^HTTP/",$c)||preg_match("/^P3P/",$c)||preg_match("/^([A-Za-z/-]+)/:/s*(.*)$/",$c)){
  86. continue;
  87. }
  88. if(trim($c)==""){
  89. continue;
  90. }
  91. $ret.=$c;
  92. }
  93. fclose($fp);
  94. return$ret;
  95. }
  96. }
  97. /**
  98. *使用POST方法发送HTTP请求
  99. *
  100. *@paramstring$url需要请求的URL,完整URL,例如:http://www.example.com:8080/test.php?parm1=var1&parm2=var2
  101. *@paramarray$vars需要POST提交的变量数组
  102. *@paramarray/string$cookies如果有COOKIE数据可以发送过去,可以是Cookie数组,也可以是Cookie字符串
  103. *@returnmixed成功返回GET回来的数据,失败返回false
  104. */
  105. functionhttp_post($url,$vars=array(),$cookies=array()){
  106. /**
  107. *使用cURL处理POST请求
  108. */
  109. if(function_exists('curl_init')){
  110. //组织COOKIE数据
  111. $header=array();
  112. if(!emptyempty($cookies)){
  113. if(is_array($cookies)){
  114. $encoded='';
  115. while(list($k,$v)=each($cookies)){
  116. $encoded.=($encoded?";":"");
  117. $encoded.=rawurlencode($k)."=".rawurlencode($v);
  118. }
  119. $header=array("Cookie:".$encoded);
  120. }elseif(is_string($cookies)){
  121. if(strtolower(substr($cookies,0,7))=='cookie:'){
  122. $header=array($cookies);
  123. }else{
  124. $header=array("Cookie:".$cookies);
  125. }
  126. }
  127. }
  128. //执行POST请求
  129. $ch=curl_init();
  130. curl_setopt($ch,CURLOPT_URL,$url);
  131. curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
  132. curl_setopt($ch,CURLOPT_POST,1);
  133. curl_setopt($ch,CURLOPT_POSTFIELDS,$vars);
  134. curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  135. $data=curl_exec($ch);
  136. curl_close($ch);
  137. if($data)
  138. return$data;
  139. else
  140. returnfalse;
  141. }
  142. /**
  143. *使用fsockopen处理POST请求
  144. */
  145. else{
  146. //组织COOKIE数据
  147. $cookie='';
  148. if(!emptyempty($cookies)){
  149. if(is_array($cookies)){
  150. $encoded='';
  151. while(list($k,$v)=each($cookies)){
  152. $encoded.=($encoded?";":"");
  153. $encoded.=rawurlencode($k)."=".rawurlencode($v);
  154. }
  155. $cookie=$encoded;
  156. }elseif(is_string($cookies)){
  157. if(strtolower(substr($cookies,0,7))=='cookie:'){
  158. $cookie=substr($cookies,7);
  159. }else{
  160. $cookie=$cookies;
  161. }
  162. }
  163. }
  164. //组织POST数据
  165. $post='';
  166. if(!emptyempty($vars)){
  167. if(is_array($vars)){
  168. $encoded='';
  169. while(list($k,$v)=each($vars)){
  170. $encoded.=($encoded?"&":"");
  171. $encoded.=rawurlencode($k)."=".rawurlencode($v);
  172. }
  173. $post=$encoded;
  174. }else{
  175. $post=$vars;
  176. }
  177. }
  178. //处理请求
  179. $url=parse_url($url);
  180. if(strtolower($url['scheme'])!='http'&&$url['scheme']!=''){
  181. returnfalse;
  182. }
  183. if(!($fp=fsockopen($url['host'],$url['port']?$url['port']:80,$errno,$errstr,10))){
  184. returnfalse;
  185. }
  186. fputs($fp,sprintf("POST%s%s%sHTTP/1.0/n",$url['path'],$url['query']?"?":"",$url['query']));
  187. fputs($fp,"Host:$url[host]/n");
  188. fputs($fp,"User-Agent:HFHttp-Client/n");
  189. if($cookie!=''){
  190. fputs($fp,"Cookie:$cookie/n/n");
  191. }
  192. fputs($fp,"Content-type:application/x-www-form-urlencoded/n");
  193. fputs($fp,"Content-length:".strlen($post)."/n");
  194. fputs($fp,"Connection:close/n/n");
  195. fputs($fp,"$post/n");
  196. $ret='';
  197. while(!feof($fp)){
  198. $c=fgets($fp,1024);
  199. if(preg_match("/^HTTP/",$c)||preg_match("/^P3P/",$c)||preg_match("/^([A-Za-z/-]+)/:/s*(.*)$/",$c)){
  200. continue;
  201. }
  202. if(trim($c)==""){
  203. continue;
  204. }
  205. $ret.=$c;
  206. }
  207. fclose($fp);
  208. return$ret;
  209. }
  210. }

你可能感兴趣的:(post)