最近两天项目需要,由于服务器正在开发,客户端进度稍快一些,没有服务器进行联调。因此我重操旧业,用PHP快速的写了一些web页面,算是当测试桩程序了,七八个web接口,基本上5到6个小时搞定了。由于当前的服务器需要与其他服务器进行对接,因此写的这个web服务还需要充当client角色,向其他服务器发送请求。

在网上搜了一下,基本上两种方法:(转自网友文章)

1.通过curl函数

$post_data = array () ;
$post_data [ ' clientname ' ] = " test08 " ;
$post_data [ ' clientpasswd ' ] = " test08 " ;
$post_data [ ' submit ' ] = " submit " ;
$url = ' http://xxx.xxx.xxx.xx/xx/xxx/top.php ' ;
$o = "" ;
foreach ( $post_data as $k => $v ) 

     $o .= " $k = " . urlencode ( $v ) . " & " ;

$post_data = substr ( $o , 0 ,- 1 ) ;
$ch = curl_init () ;
curl_setopt ( $ch , CURLOPT_POST , 1 ) ;
curl_setopt ( $ch , CURLOPT_HEADER , 0 ) ;
curl_setopt ( $ch , CURLOPT_URL , $url ) ;
//为了支持cookie 
curl_setopt ( $ch , CURLOPT_COOKIEJAR , ' cookie.txt ' ) ;
curl_setopt ( $ch , CURLOPT_POSTFIELDS , $post_data ) ;
$result = curl_exec ( $ch ) ;
2. 通过fsockopen
$URL =‘ http : //xxx.xxx.xxx.xx/xx/xxx/top.php'; 
$post_data [ ' clientname ' ] = " test08 " ;
$post_data [ ' clientpasswd ' ] = " test08 " ;
$post_data [ ' submit ' ] = " ログイン " ;
$referrer = "" ;
// parsing the given URL 
$URL_Info = parse_url ( $URL ) ;
// Building referrer 
if ( $referrer == "" ) // if not given use this script as referrer 
$referrer = $_SERVER [ " SCRIPT_URI " ] ;
 
// making string from $data 
foreach ( $post_data as $key => $value ) 
$values [] = " $key = " . urlencode ( $value ) ;
 
$data_string = implode ( " & " , $values ) ;
// Find out which port is needed - if not given use standard (=80) 
if ( ! isset ( $URL_Info [ " port " ])) 
$URL_Info [ " port " ] = 80 ;
// building POST-request: 
$request .= " POST " . $URL_Info [ " path " ] . " HTTP/1.1 /n " ;
$request .= " Host: " . $URL_Info [ " host " ] . " /n " ;
$request .= " Referer: $referrer /n " ;
$request .= " Content-type: application/x-www-form-urlencoded /n " ;
$request .= " Content-length: " . strlen ( $data_string ) . " /n " ;
$request .= " Connection: close /n " ;
$request .= " /n " ;
$request .= $data_string . " /n " ;
$fp = fsockopen ( $URL_Info [ " host " ] , $URL_Info [ " port " ]) ;
fputs ( $fp , $request ) ;
while ( ! feof ( $fp )) { 
     $result .= fgets ( $fp , 128 ) ;

fclose ( $fp ) ;
 
上述两种方法没有封装成通用的function,我验证了方法2,发现基本上能实现功能,但有两个很大的缺点:
1. 延迟比较大,我在内网测试,用命令行进行的测试,发现响应有时候需要2S以上,不能接受。
2. 读到的response包含了HTTP头域信息,但是大部分情况下,我们只需要body部分内容,要提取body内容,还需要一番处理。
方法1感觉也很笨拙,似乎也很难满足我的需求,最后忘记在哪个英文网站上找到了下面这个方法:
  function do_post_request($url, $data, $optional_headers = null)
  {
     $params = array('http' => array(
                  'method' => 'POST',
                  'content' => $data
               ));
     if ($optional_headers !== null) {
        $params['http']['header'] = $optional_headers;
     }
     $ctx = stream_context_create($params);
     $fp = @fopen($url, 'rb', false, $ctx);
     if (!$fp) {
        throw new Exception("Problem with $url, $php_errormsg");
     }
     $response = @stream_get_contents($fp);
     if ($response === false) {
        throw new Exception("Problem reading data from $url, $php_errormsg");
     }
     return $response;
  }
 
试用了一下,感觉效果非常好,简洁,通用,而且返回的内容仅仅是Body中内容。我也没去仔细研究这些API的说明,就采用了这个,没发现有大的问题。如果有人有类似需求,推荐大家采用这个function.
 
2018年7月24日
还有一个呢

function do_post_request3 ($url, $params = array()) {
$data_string = json_encode($params);
$c = curl_init();
curl_setopt($c, CURLOPT_URL, $url);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($c, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($c, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($data_string)));
$rsp = curl_exec($c);
curl_close($c);
return json_decode($rsp, true);
}

php 模拟post的新发现,重点在最后的新方法的更多相关文章

  1. 重点:怎样正确的使用QThread类(注:包括推荐使用QThread线程的新方法QObject::moveToThread)

    背景描述: 以前,继承 QThread 重新实现 run() 函数是使用 QThread唯一推荐的使用方法.这是相当直观和易于使用的.但是在工作线程中使用槽机制和Qt事件循环时,一些用户使用错了.Qt ...

  2. Android模拟、实现、触发系统按键事件的方法

     Android模拟.实现.触发系统按键事件的方法 /** * 模拟系统按键. * * @param keyCode */ public static void onKeyEvent(final ...

  3. SWF在线绘本批量制作高质量PDF的新方法(重点在批量制作)

    SWF在线绘本批量制作高质量PDF的新方法(重点在批量制作) 2012-12-21  未来决定...       http://www.ebama.net/thread-107643-1-1.html ...

  4. php模拟用户自动在qq空间发表文章的方法

    我们这里是一个简单的利用php来模拟登录后再到QQ空间发送文章的一个简单的程序,有需要的朋友可以参考,或改进可以给我意见,代码如下: <?php //模拟get post请求函数 http:// ...

  5. 模拟做饭系统(java+线程中的join方法)

    (一)项目框架分析 妈妈要去做饭,发现没有酱油,让儿子去买酱油,然后回来做饭. 根据面向对象的思想,有两个对象,妈妈和儿子 主要有两个方法: (一)没有线程控制(即儿子没有买酱油回来妈妈就做好饭了)+ ...

  6. 使用curl模拟ip和来源进行网站采集的实现方法

    对于限制了ip和来源的网站,使用正常的采集方式是不行的.本文将介绍一种方法,使用php的curl类实现模拟ip和来源,实现采集限制ip和来源的网站. 1.设置页面限制ip和来源访问 server.ph ...

  7. curl模拟ip和来源进行网站采集的实现方法

    对于限制了ip和来源的网站,使用正常的采集方式是不行的.这里说我的一种方法吧,使用php的curl类实现模拟ip和来源,可以实现采集限制ip和来源的网站. 1.设置页面限制ip和来源访问比如服务端的s ...

  8. C#三种模拟自动登录和提交POST信息的实现方法【转】

    网页自动登录(提交Post内容)的用途很多,如验证身份.程序升级.网络投票等,以下是用C#实现的方法.       网页自动登录和提交POST信息的核心就是分析网页的源代码(HTML),在C#中,可以 ...

  9. 使用JAVA实现模拟登陆并发送新浪微博(非调用新浪API)

    没有调用新浪的API,在程序中加入自己的帐号和密码就能发送微博,代码完全在后台运行,不用打开浏览器. 用了HtmlUnit这个库来模拟登录还有发送微博. 先上效果图: 这个是刚登陆上获取第一页的信息. ...

随机推荐

  1. go-web编程之处理xml

    摘抄自astaxie的开源书籍 build-web-application-with-golang 接下来的例子以下面XML描述的信息进行操作. <?xml version="1.0& ...

  2. linux网络防火墙-iptables基础详解

    一:前言 防火墙,其实说白了讲,就是用于实现Linux下访问控制的功能的,它分为硬件的或者软件的防火墙两种.无论是在哪个网络中,防火墙工作的地方一定是在网络的边缘.而我们的任务就是需要去定义到底防火墙 ...

  3. python3操作Excel

    1.安装openpyxl模块: 在cmd命令窗执行命令 pip install openpyxl    安装openpyxl模块 from openpyxl import load_workbook ...

  4. SpringCloud核心教程 | 第一篇: 使用Intellij中的Spring Initializr来快速构建Spring Cloud工程

    spring cloud简介 spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.微代理.事件总线.全局锁.决策竞选.分布式会话等等.它运行环 ...

  5. 全球可信并且唯一免费的HTTPS(SSL)证书颁发机构:StartSSL

    全球可信并且唯一免费的HTTPS(SSL)证书颁发机构:StartSSL http://blog.s135.com/startssl/ 购买权威机构的证书一年大概得七八千元,其实这是不值得的,所以一直 ...

  6. 软件——protel 的pcb电路图制作

    近期一直在学习PCB板的绘制.

  7. Vue 自定义全局消息框组件

    消息弹框组件,默认3秒后自动关闭,可设置info/success/warning/error类型 效果图: 文件目录: Message.vue <template> <transit ...

  8. (错误记录)Vue: Unknown custom element

    错误: vue.js:634 [Vue warn]: Unknown custom element: <ve-pie> - did you register the component c ...

  9. Centos7安装.Net Core 2.2环境以及部署.Net Core MVC程序(Apache+Jexus环境)

    原文:Centos7安装.Net Core 2.2环境以及部署.Net Core MVC程序(Apache+Jexus环境) 1.双11抢购***VPS.配置如下: CPU:2 核 内存:2048 M ...

  10. 51Nod——N1082 与7无关的数

    https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1082 题目来源: 有道难题 基准时间限制:1 秒 空间限制:13107 ...