php发送post请求的三种方法


Posted in PHP onFebruary 11, 2014

方法一:

/**
 * 发送post请求
 * @param string $url 请求地址
 * @param array $post_data post键值对数据
 * @return string
 */
function send_post($url, $post_data) {

  $postdata = http_build_query($post_data);
  $options = array(
    'http' => array(
      'method' => 'POST',
      'header' => 'Content-type:application/x-www-form-urlencoded',
      'content' => $postdata,
      'timeout' => 15 * 60 // 超时时间(单位:s)
    )
  );
  $context = stream_context_create($options);
  $result = file_get_contents($url, false, $context);

  return $result;
}

//使用方法
$post_data = array(
  'username' => 'stclair2201',
  'password' => 'handan'
);
send_post('https://3water.com', $post_data);

方法二:Socket版本

<?php
/**
 * Socket版本
 * 使用方法:
 * $post_string = "app=socket&version=beta";
 * request_by_socket('chajia8.com', '/restServer.php', $post_string);
 */
function request_by_socket($remote_server,$remote_path,$post_string,$port = 80,$timeout = 30) {
  $socket = fsockopen($remote_server, $port, $errno, $errstr, $timeout);
  if (!$socket) die("$errstr($errno)");
  fwrite($socket, "POST $remote_path HTTP/1.0");
  fwrite($socket, "User-Agent: Socket Example");
  fwrite($socket, "HOST: $remote_server");
  fwrite($socket, "Content-type: application/x-www-form-urlencoded");
  fwrite($socket, "Content-length: " . (strlen($post_string) + 8) . "");
  fwrite($socket, "Accept:*/*");
  fwrite($socket, "");
  fwrite($socket, "mypost=$post_string");
  fwrite($socket, "");
  $header = "";
  while ($str = trim(fgets($socket, 4096))) {
    $header .= $str;
  }

  $data = "";
  while (!feof($socket)) {
    $data .= fgets($socket, 4096);
  }

  return $data;
}
?>

方法三:Curl版本

<?php
/**
 * Curl版本
 * 使用方法:
 * $post_string = "app=request&version=beta";
 * request_by_curl('https://3water.com/restServer.php', $post_string);
 */
function request_by_curl($remote_server, $post_string) {
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $remote_server);
  curl_setopt($ch, CURLOPT_POSTFIELDS, 'mypost=' . $post_string);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_USERAGENT, "3water.com's CURL Example beta");
  $data = curl_exec($ch);
  curl_close($ch);

  return $data;
}
?>

下面是其他网友的方法:

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){//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;
  }
}

大家可以根据需要选择适合自己的即可。

PHP 相关文章推荐
javascript 小型动画组件与实现代码
Jun 02 PHP
PHP的范围解析操作符(::)的含义分析说明
Jul 03 PHP
PHP隐形一句话后门,和ThinkPHP框架加密码程序(base64_decode)
Nov 02 PHP
PHP __autoload()方法真的影响性能吗?
Mar 30 PHP
php字符串按照单词进行反转的方法
Mar 14 PHP
PHP编写学校网站上新生注册登陆程序的实例分享
Mar 21 PHP
PHPCMS V9 添加二级导航的思路详解
Oct 20 PHP
PHP随机获取未被微信屏蔽的域名(微信域名检测)
Mar 19 PHP
PHP判断json格式是否正确的实现代码
Sep 20 PHP
微信支付之JSAPI公众号支付详解
May 15 PHP
gearman管理工具GearmanManager的安装与php使用方法示例
Feb 27 PHP
Docker 安装 PHP并与Nginx的部署实例讲解
Feb 27 PHP
codeigniter教程之多文件上传使用示例
Feb 11 #PHP
php创建sprite
Feb 11 #PHP
PHP循环结构实例讲解
Feb 10 #PHP
更改localhost为其他名字的方法
Feb 10 #PHP
php 获取SWF动画截图示例代码
Feb 10 #PHP
php导入csv文件碰到乱码问题的解决方法
Feb 10 #PHP
php判断正常访问和外部访问的示例
Feb 10 #PHP
You might like
php ios推送(代码)
2013/07/01 PHP
php获取网页中图片、DIV内容的简单方法
2014/06/19 PHP
教你在PHPStorm中配置Xdebug
2015/07/27 PHP
PHP中substr_count()函数获取子字符串出现次数的方法
2016/01/07 PHP
PHP+swoole实现简单多人在线聊天群发
2016/01/19 PHP
mysql_escape_string()函数用法分析
2016/04/25 PHP
php使用parse_str实现查询字符串解析到变量中的方法
2017/02/17 PHP
写的htc的数据表格
2007/01/20 Javascript
js获取上传文件大小示例代码
2014/04/10 Javascript
node.js中的fs.close方法使用说明
2014/12/17 Javascript
js实现带按钮的上下滚动效果
2015/05/12 Javascript
干货分享:让你分分钟学会javascript闭包
2015/12/25 Javascript
js轮盘抽奖实例分析
2020/04/17 Javascript
原生js编写焦点图效果
2016/12/08 Javascript
微信小程序 UI与容器组件总结
2017/02/21 Javascript
nodejs个人博客开发第一步 准备工作
2017/04/12 NodeJs
vue.js加载新的内容(实例代码)
2017/06/01 Javascript
解决微信小程序防止无法回到主页的问题
2018/09/28 Javascript
通过js示例讲解时间复杂度与空间复杂度
2019/08/06 Javascript
微信小程序实现通讯录列表展开收起
2020/11/18 Javascript
详解Django+Uwsgi+Nginx的生产环境部署
2018/06/25 Python
对DJango视图(views)和模版(templates)的使用详解
2019/07/17 Python
python支付宝支付示例详解
2019/08/22 Python
python打印文件的前几行或最后几行教程
2020/02/13 Python
澳大利亚领先的皮肤诊所:Skin Matrix(抗衰老、痤疮专家、药妆护肤)
2018/05/20 全球购物
蒙蒂塞罗商店:Monticello Shop
2018/11/25 全球购物
static全局变量与普通的全局变量有什么区别?static局部变量和普通局部变量有什么区别?static函数与普通函数有什么区别?
2015/02/22 面试题
委托公证书范本
2014/04/03 职场文书
给校长的建议书400字
2014/05/15 职场文书
员工规章制度范本
2015/08/07 职场文书
JavaScript offset实现鼠标坐标获取和窗口内模块拖动
2021/05/30 Javascript
Python连接Postgres/Mysql/Mongo数据库基本操作大全
2021/06/29 Python
Python获取江苏疫情实时数据及爬虫分析
2021/08/02 Python
Python 可迭代对象 iterable的具体使用
2021/08/07 Python
拙作再改《我的收音机情缘》
2022/04/05 无线电
小喇叭开始广播了! 四十多年前珍贵老照片
2022/05/09 无线电