免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1565 | 回复: 0
打印 上一主题 下一主题

php 模拟post请求 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-07-16 14:03 |只看该作者 |倒序浏览
post
  1. class Request{
  2.   
  3.     public static function post($url, $post_data = '', $timeout = 5){//curl
  4.   
  5.         $ch = curl_init();
  6.         curl_setopt ($ch, CURLOPT_URL, $url);
  7.   
  8.         curl_setopt ($ch, CURLOPT_POST, 1);
  9.   
  10.         if($post_data != ''){
  11.   
  12.             curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
  13.   
  14.         }
  15.   
  16.         curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
  17.   
  18.         curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  19.   
  20.         curl_setopt($ch, CURLOPT_HEADER, false);
  21.   
  22.         $file_contents = curl_exec($ch);
  23.   
  24.         curl_close($ch);
  25.   
  26.         return $file_contents;
  27.   
  28.     }
  29.     public static function post2($url, $data=array()){//file_get_content
  30.             
  31.         $postdata = http_build_query(
  32.             $data
  33.         );           
  34.         $opts = array('http' =>
  35.   
  36.                       array(
  37.   
  38.                           'method'  => 'POST',
  39.   
  40.                           'header'  => 'Content-type: application/x-www-form-urlencoded',
  41.   
  42.                           'content' => $postdata
  43.   
  44.                       )
  45.   
  46.         );           
  47.         $context = stream_context_create($opts);
  48.         $result = file_get_contents($url, false, $context);
  49.         return $result;
  50.     }
  51.     public static function post3($host,$path,$query,$others=''){//fsocket
  52.   
  53.   
  54.         $post="POST $path HTTP/1.1\r\nHost: $host\r\n";
  55.   
  56.         $post.="Content-type: application/x-www-form-";
  57.   
  58.         $post.="urlencoded\r\n${others}";
  59.   
  60.         $post.="User-Agent: Mozilla 4.0\r\nContent-length: ";
  61.   
  62.         $post.=strlen($query)."\r\nConnection: close\r\n\r\n$query";
  63.   
  64.         $h=fsockopen($host,80);
  65.   
  66.         fwrite($h,$post);
  67.   
  68.         for($a=0,$r='';!$a;){
  69.   
  70.                 $b=fread($h,8192);
  71.   
  72.                 $r.=$b;
  73.   
  74.                 $a=(($b=='')?1:0);
  75.   
  76.             }
  77.   
  78.         fclose($h);
  79.   
  80.         return $r;
  81.   
  82.     }
  83. }
  84. $url='http://******/con/Inter.php';
  85. $data=Request::post($url,array('api'=>'tag_list'));
  86. $data2=Request::post2($url,array('api'=>'tag_list'));
  87. echo $data;
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP