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 相关文章推荐
不要轻信 PHP_SELF的安全问题
Sep 05 PHP
ThinkPHP自动转义存储富文本编辑器内容导致读取出错的解决方法
Aug 08 PHP
PHP获取当前所在目录位置的方法
Nov 26 PHP
dedecms集成财付通支付接口
Dec 28 PHP
汇总PHPmailer群发Gmail的常见问题
Feb 24 PHP
php metaphone()函数及php localeconv() 函数实例解析
May 15 PHP
Yii框架实现图片上传的方法详解
May 20 PHP
PHP基于SimpleXML生成和解析xml的方法示例
Jul 17 PHP
PHP实现将几张照片拼接到一起的合成图片功能【便于整体打印输出】
Nov 14 PHP
PHP7 mongoDB扩展使用的方法分享
May 02 PHP
PHP命名空间定义与用法实例分析
Aug 14 PHP
PHP使用openssl扩展实现加解密方法示例
Feb 20 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导出MySQL数据到Excel文件(fputcsv)
2011/07/03 PHP
用PHP写的基于Memcache的Queue实现代码
2011/11/27 PHP
php实现批量下载百度云盘文件例子分享
2014/04/10 PHP
php使用Jpgraph绘制饼状图的方法
2015/06/10 PHP
PHP中使用CURL发送get/post请求上传图片批处理功能
2018/10/15 PHP
详解在YII2框架中使用UEditor编辑器发布文章
2018/11/02 PHP
PHP实现简单注册登录系统
2020/12/28 PHP
javascript 网页跳转的方法
2008/12/24 Javascript
php is_numberic函数造成的SQL注入漏洞
2014/03/10 Javascript
Javascript writable特性介绍
2015/02/27 Javascript
百度多文件异步上传控件webuploader基本用法解析
2016/11/07 Javascript
JS中cookie的使用及缺点讲解
2017/05/13 Javascript
node+vue实现用户注册和头像上传的实例代码
2017/07/20 Javascript
原生JS实现简单的无缝自动轮播效果
2018/09/26 Javascript
移动端(微信等使用vConsole调试console的方法
2019/03/05 Javascript
Vue CLI 3.x 自动部署项目至服务器的方法
2019/04/02 Javascript
在vue-cli 3中给stylus、sass样式传入共享的全局变量
2019/08/12 Javascript
layui表格分页 记录勾选的实例
2019/09/02 Javascript
Python对象的深拷贝和浅拷贝详解
2014/08/25 Python
在Python程序中操作文件之flush()方法的使用教程
2015/05/24 Python
Python元字符的用法实例解析
2018/01/17 Python
Python 抓取微信公众号账号信息的方法
2019/06/14 Python
python实现比对美团接口返回数据和本地mongo数据是否一致示例
2019/08/09 Python
Windows下pycharm创建Django 项目(虚拟环境)过程解析
2019/09/16 Python
python实现名片管理器的示例代码
2019/12/17 Python
pytorch 实现cross entropy损失函数计算方式
2020/01/02 Python
Python异常原理及异常捕捉实现过程解析
2020/03/25 Python
基于python实现数组格式参数加密计算
2020/04/21 Python
HTML5有哪些新特征
2015/12/01 HTML / CSS
html5移动端价格输入键盘的实现
2019/09/16 HTML / CSS
一套Delphi的笔试题一
2016/02/14 面试题
中文专业毕业生自荐信
2013/10/28 职场文书
公司股东合作协议书
2014/09/14 职场文书
开学典礼校长致辞
2015/07/29 职场文书
left join、inner join、right join的区别
2021/04/05 MySQL
详细聊一聊mysql的树形结构存储以及查询
2022/04/05 MySQL