引用:http://blog.sjzycxx.cn/post/435/

1.使用 file_get_contents()

/**
* 发送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;
}

//使用方法,需要2个参数,URL和参数,post的参数得自己构造,是数组的格式
$post_data = array(
      'username' => 'stclair2201',
      'password' => 'handan'
);
send_post('http://www.qianyunlai.com', $post_data);

2.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;
}
?>

3.Curl版本

<?php
/**
* Curl版本
* 使用方法:
* $post_string = "app=request&version=beta";
* request_by_curl('http://www.qianyunlai.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, "qianyunlai.com's CURL Example beta");
     $data = curl_exec($ch);
     curl_close($ch);

return $data;
}
?>

php发送post请求的三种方法的更多相关文章

  1. php发送post请求的三种方法示例

    本文分享下php发送post请求的三种方法与示例代码,分别使用curl.file_get_content.fsocket来实现post提交数据,大家做个参考. php发送post请求的三种方法,分别使 ...

  2. 【MySQL】锁——查看当前数据库锁请求的三种方法 20

    MySQL提供了查看当前数据库锁请求的三种方法:1. show  full  processlist命令  观察state和info列 2. show engine  innodb status\G ...

  3. 使用jmeter发送put请求的三种方式

    之前在前公司使用jmeter调试接口时,由于都是get和post请求,所以一直是顺风顺水的,毫无阻拦在短时间内调试完所有接口. 但是呢,在换到新公司后,发现接口请求是多式多样的,get.post必须有 ...

  4. vue中数据请求的三种方法

    注意请求可能存在跨域问题,需要去配置好 这三种建议使用axios 1.resource Vue 要实现异步加载需要使用到 vue-resource 库. Vue.js 2.0 版本推荐使用 axios ...

  5. Python使用requests发送post请求的三种方式

    1.我们使用postman进行接口测试的时候,发现POST请求方式的编码有3种,具体的编码方式如下: A:application/x-www-form-urlencoded ==最常见的post提交数 ...

  6. PHP发送HTTP请求的6种方法

    方法1: 用 file_get_contents 以get方式获取内容: <?php$url = 'https://wenda.shukaiming.com/';echo file_get_co ...

  7. PHP发送POST请求的三种方式

    class Request{ public static function post($url, $post_data = '', $timeout = 5){//curl $ch = curl_in ...

  8. php发送get、post请求的6种方法代码示例

    本文主要展示了php发送get.post请求的6种方法的代码示例,分别为使用file_get_contents .fopen.fsockopen.curl来发送GET和POST请求,代码如下: 方法1 ...

  9. SpringMVC的请求转发的三种方法

    SpringMVC请求转发的三种方法 首先明白请求转发是一次请求,地址栏不会发生变化,区别于重定向.springmvc环境自行配置. 以下举例中存在如下文件/WEB-INF/pages/success ...

随机推荐

  1. iOS Development和iOS Distribution有什么区别

    http://zhidao.baidu.com/link?url=T9od33JuA7jjxzfyV-wOjuVLSNUaqpc9aoCu2HjfYfHBuRLW1CNDii0Bh9uvG6h-GeJ ...

  2. AndroidPn服务端部分bug解决方案

    目前推送的情况已经大致可以了,可以正常推送.但是要在实际生产中使用,要改进很多地方. 原本的版本,是不会对消息重新发送的.消息如果丢失,或者用户没有在线,消息也不会重新的发送.所以,这些问题都是要解决 ...

  3. 移动端Retina屏边框线1px 显示为2px或3px问题解决方法

    我们在开发移动端web项目时经常遇到设置border:1px,但是显示的边框却为2px或是3px粗细,这是因为设备像素比devicePixelRatio为2或3引起的.   1.何为“设备像素比dev ...

  4. JS和C#访问遇到QueryInterface调用出错

    在原来的WinForm里,我们只要在窗体类的头部添加属性[System.Runtime.InteropServices.ComVisibleAttribute(true)],然后 webBrowser ...

  5. 使用node_redis进行redis数据库crud操作

    正在学习使用pomelo开发游戏服务器,碰到node.js操作redis,记录一下 假设应用场景是操作一个用户表的数据 引入node_redis库,创建客户端 var redis = require( ...

  6. Web Server IIS7部署网站常遇到的错误及解决办法

    IIS7部署网站常遇到的错误及解决办法 经常遇到问题: 1.错误:403.14-Forbidden Web 服务器被配置为不列出此目录的内容及Login on failed for "IIS ...

  7. JQuery Mobile - 解决切换页面时,闪屏,白屏等问题

    在点击链接,切换页面时候,总是闪屏,感觉很别扭,看起来不舒服,怎么解决这个问题?方法很简单,就是在每个页面的meta标签内定义user-scalable的属性为 no! <meta name=& ...

  8. iOS 多Target, Other link Flag

    在创建多个马甲包或者多个App间只有很小的差异是使用多Target是一种很好的方法 https://www.jianshu.com/p/18db54655246 1:选中原始的Target, 点击右键 ...

  9. java实现fp-growth算法

    本文参考韩家炜<数据挖掘-概念与技术>一书第六章,前提条件要理解 apriori算法. 另外一篇写得较好的文章在此推荐: http://hi.baidu.com/nefzpohtpndho ...

  10. RF射频技术的原理

    [摘要]射频技术(RF)是Radio Frequency的缩写.较常见的应用有无线射频识别(Radio Frequency Identification,RFID),常称为感应式电子晶片或近接卡.感应 ...