登录  /  注册
php实现模拟post请求用法实例_php技巧
php中文网
发布: 2016-05-16 20:11:02
原创
671人浏览过

本文实例讲述了php实现模拟post请求的方法。分享给大家供大家参考。具体如下:

class Request{
 public static function post($url, $post_data = '', $timeout = 5){//curl
  $ch = curl_init(); 
  curl_setopt ($ch, CURLOPT_URL, $url);
  curl_setopt ($ch, CURLOPT_POST, 1);
  if($post_data != ''){
   curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
  }
  curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
  curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  curl_setopt($ch, CURLOPT_HEADER, false);
  $file_contents = curl_exec($ch);
  curl_close($ch);
  return $file_contents;
 } 
 public static function post2($url, $data=array()){//file_get_content
  $postdata = http_build_query(
   $data
  );   
  $opts = array('http' =>
      array(
       'method' => 'POST',
       'header' => 'Content-type: application/x-www-form-urlencoded',
       'content' => $postdata
      )
  );   
  $context = stream_context_create($opts);
  $result = file_get_contents($url, false, $context); 
  return $result;
 } 
 public static function post3($host,$path,$query,$others=''){//fsocket
  $post="POST $path HTTP/1.1\r\nHost: $host\r\n";
  $post.="Content-type: application/x-www-form-";
  $post.="urlencoded\r\n${others}";
  $post.="User-Agent: Mozilla 4.0\r\nContent-length: ";
  $post.=strlen($query)."\r\nConnection: close\r\n\r\n$query";
  $h=fsockopen($host,80);
  fwrite($h,$post);
  for($a=0,$r='';!$a;){
    $b=fread($h,8192);
    $r.=$b;
    $a=(($b=='')?1:0);
   }
  fclose($h);
  return $r;
 }
}
$url='http://******/con/Inter.php';
$data=Request::post($url,array('api'=>'tag_list'));
$data2=Request::post2($url,array('api'=>'tag_list'));
echo $data;

登录后复制

希望本文所述对大家的php程序设计有所帮助。

相关标签:
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 技术文章
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2023 //m.sbmmt.com/ All Rights Reserved | 苏州跃动光标网络科技有限公司 | 苏ICP备2020058653号-1

 | 本站CDN由 数掘科技 提供

登录PHP中文网,和优秀的人一起学习!
全站2000+教程免费学