php 模拟post的新发现,重点在最后的新方法
最近两天项目需要,由于服务器正在开发,客户端进度稍快一些,没有服务器进行联调。因此我重操旧业,用PHP快速的写了一些web页面,算是当测试桩程序了,七八个web接口,基本上5到6个小时搞定了。由于当前的服务器需要与其他服务器进行对接,因此写的这个web服务还需要充当client角色,向其他服务器发送请求。
在网上搜了一下,基本上两种方法:(转自网友文章)
1.通过curl函数
$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 ) ;
$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 ) ;
{
$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;
}
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的新发现,重点在最后的新方法的更多相关文章
- 重点:怎样正确的使用QThread类(注:包括推荐使用QThread线程的新方法QObject::moveToThread)
背景描述: 以前,继承 QThread 重新实现 run() 函数是使用 QThread唯一推荐的使用方法.这是相当直观和易于使用的.但是在工作线程中使用槽机制和Qt事件循环时,一些用户使用错了.Qt ...
- Android模拟、实现、触发系统按键事件的方法
Android模拟.实现.触发系统按键事件的方法 /** * 模拟系统按键. * * @param keyCode */ public static void onKeyEvent(final ...
- SWF在线绘本批量制作高质量PDF的新方法(重点在批量制作)
SWF在线绘本批量制作高质量PDF的新方法(重点在批量制作) 2012-12-21 未来决定... http://www.ebama.net/thread-107643-1-1.html ...
- php模拟用户自动在qq空间发表文章的方法
我们这里是一个简单的利用php来模拟登录后再到QQ空间发送文章的一个简单的程序,有需要的朋友可以参考,或改进可以给我意见,代码如下: <?php //模拟get post请求函数 http:// ...
- 模拟做饭系统(java+线程中的join方法)
(一)项目框架分析 妈妈要去做饭,发现没有酱油,让儿子去买酱油,然后回来做饭. 根据面向对象的思想,有两个对象,妈妈和儿子 主要有两个方法: (一)没有线程控制(即儿子没有买酱油回来妈妈就做好饭了)+ ...
- 使用curl模拟ip和来源进行网站采集的实现方法
对于限制了ip和来源的网站,使用正常的采集方式是不行的.本文将介绍一种方法,使用php的curl类实现模拟ip和来源,实现采集限制ip和来源的网站. 1.设置页面限制ip和来源访问 server.ph ...
- curl模拟ip和来源进行网站采集的实现方法
对于限制了ip和来源的网站,使用正常的采集方式是不行的.这里说我的一种方法吧,使用php的curl类实现模拟ip和来源,可以实现采集限制ip和来源的网站. 1.设置页面限制ip和来源访问比如服务端的s ...
- C#三种模拟自动登录和提交POST信息的实现方法【转】
网页自动登录(提交Post内容)的用途很多,如验证身份.程序升级.网络投票等,以下是用C#实现的方法. 网页自动登录和提交POST信息的核心就是分析网页的源代码(HTML),在C#中,可以 ...
- 使用JAVA实现模拟登陆并发送新浪微博(非调用新浪API)
没有调用新浪的API,在程序中加入自己的帐号和密码就能发送微博,代码完全在后台运行,不用打开浏览器. 用了HtmlUnit这个库来模拟登录还有发送微博. 先上效果图: 这个是刚登陆上获取第一页的信息. ...
随机推荐
- 如果笔记本的 WIN7 运行很卡,请尝试运行这些批处理
如果笔记本的 WIN7 运行很卡,请尝试运行这些批处理 WIN7是不是很卡?关掉下列服务吧 @echo off rem AppXSvc 为部署应用商店应用程序提供基础结构支持 rem BITS 微软的 ...
- 35.Intellij IDEA设置忽略部分类编译错误
转自:https://www.aliyun.com/jiaocheng/290360.html 有些时候我们的项目中有些错误,但这些错误并不影响项目的整体运行(或许是没有使用到),默认情况下idea是 ...
- 4.使用 WSDL 指定的标准 SOAP 消息格式
转自:https://technet.microsoft.com/zh-cn/sysinternals/x2ccke44(v=vs.94) 为 XML 文档(定义 Web 服务)定义架构的行业标准 W ...
- Zabbix主动代理模式 + 主动模式agent客户端
2.1.1 安装软件 ]# rpm -qa zabbix* zabbix-proxy-sqlite3-3.4.15-1.el7.x86_64 zabbix-proxy-mysql-3.4.15-1.e ...
- Objective-C基础笔记(8)Foundation常用类NSString
一.创建字符串的方法 void stringCreate(){ //方法1 NSString *str1 = @"A String!"; //方法2 NSString *str2 ...
- 业余学习react 学习记录
http://www.ruanyifeng.com/blog/2015/03/react (阮一峰 react 学习) 1.搭建环境:npm 使用 React npm install -g cnpm ...
- mysql-cacti-templates-1.1.2.tar.gz 免费下载 cacti MySQL添加监控
cacti MySQL添加监控 1. 安装监控插件 wget http://mysql-cacti-templates.googlecode.com/files/mysql-cacti-templat ...
- amazeui学习笔记二(进阶开发1)--项目结构structure
amazeui学习笔记二(进阶开发1)--项目结构structure 一.总结 1.项目结构:是说的amazeui在github上面的项目结构,二次开发amazeui用 二.项目结构structure ...
- 16、cgminer学习之:popen函数和system函数详解(执行系统命令)
1.popen函数我们先用man指令查一下popen函数: 函数说明: (1)popen()会调用fork()产生子进程,然后从子进程中调用/bin/sh -c来执行参数command的指令. (2) ...
- Project Euler 516 5-smooth totients (数论)
题目链接: https://projecteuler.net/problem=516 题目: \(5\)-smooth numbers are numbers whose largest prime ...