php 模擬post請求

WBOY
發布: 2016-07-25 08:44:10
原創
983 人瀏覽過
  1. class Request{
  2. public static function post($url, $post_data = '', $timeout = 5){//curl
  3. $ch = curl_init();
  4. curl_setopt ($ch, CURLOPT_URL, $url);
  5. curl_setopt ($ch, CURLOPT_POST, 1);
  6. if($post_data != ''){
  7. curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
  8. }
  9. curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
  10. curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  11. curl_setopt($ch, CURLOPT_HEADER, false);
  12. $file_contents = curl_exec($ch);
  13. curl_close($ch);
  14. return $file_contents;
  15. }
  16. public static function post2($url, $data=array()){//file_get_content
  17. $postdata = http_build_query(
  18. $data
  19. );
  20. $opts = array('http' =>
  21. array(
  22. 'method' => 'POST',
  23. 'header' => 'Content-type: application/x-www-form-urlencoded',
  24. 'content' => $postdata
  25. )
  26. );
  27. $context = stream_context_create($opts);
  28. $result = file_get_contents($url, false, $context);
  29. return $result;
  30. }
  31. public static function post3($host,$path,$query,$others=''){//fsocket
  32. $post="POST $path HTTP/1.1\r\nHost: $host\r\n";
  33. $post.="Content-type: application/x-www-form-";
  34. $post.="urlencoded\r\n${others}";
  35. $post.="User-Agent: Mozilla 4.0\r\nContent-length: ";
  36. $post.=strlen($query)."\r\nConnection: close\r\n\r\n$query";
  37. $h=fsockopen($host,80);
  38. fwrite($h,$post);
  39. for($a=0,$r='';!$a;){
  40. $b=fread($h,8192);
  41. $r.=$b;
  42. $a=(($b=='')?1:0);
  43. }
  44. fclose($h);
  45. return $r;
  46. }
  47. }
  48. $url='http://******/con/Inter.php';
  49. $data=Request::post($url,array('api'=>'tag_list'));
  50. $data2=Request::post2($url,array('api'=>'tag_list'));
  51. echo $data;
复制代码

php,發布


相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板