使用PHP模拟post提交数据
使用PHP模拟post提交数据
这也是个老生常谈的话题了,上午花了点时间把这个问题整理了一下。
一般来说用PHP来模拟post提交数据有三种方法,file_get_contents、curl和socket。
写了个公用函数,专门用来打印post数据:
- <?php
- function pr() {
- $params = func_get_args();
- foreach ($params as $key => $value) {
- echo "<pre>";
- print_r($value);
- echo "</pre>";
- }
- }
<?php
function pr() {
$params = func_get_args();
foreach ($params as $key => $value) {
echo "<pre>";
print_r($value);
echo "</pre>";
}
}
先写一个post.php,用来接收post数据并打印出来:
- <?php
- require dirname(__FILE__).'/function.php';
- if (isset($_POST) && !empty($_POST)) {
- pr($_POST);
- } else {
- pr("NO POST DATA!");
- }
<?php
require dirname(__FILE__).'/function.php'; if (isset($_POST) && !empty($_POST)) {
pr($_POST);
} else {
pr("NO POST DATA!");
}
下面是用file_get_contents来模拟post:
- <?php
- require dirname(__FILE__).'/function.php';
- function file_get_contents_post($url, $post) {
- $options = array(
- 'http' => array(
- 'method' => 'POST',
- // 'content' => 'name=caiknife&email=caiknife@gmail.com',
- 'content' => http_build_query($post),
- ),
- );
- $result = file_get_contents($url, false, stream_context_create($options));
- return $result;
- }
- $data = file_get_contents_post("http://www.a.com/post/post.php", array('name'=>'caiknife', 'email'=>'caiknife@gmail.com'));
- var_dump($data);
<?php
require dirname(__FILE__).'/function.php'; function file_get_contents_post($url, $post) {
$options = array(
'http' => array(
'method' => 'POST',
// 'content' => 'name=caiknife&email=caiknife@gmail.com',
'content' => http_build_query($post),
),
); $result = file_get_contents($url, false, stream_context_create($options)); return $result;
} $data = file_get_contents_post("http://www.a.com/post/post.php", array('name'=>'caiknife', 'email'=>'caiknife@gmail.com')); var_dump($data);
很简单是吧?再来看看curl模拟post:
- <?php
- require dirname(__FILE__).'/function.php';
- function curl_post($url, $post) {
- $options = array(
- CURLOPT_RETURNTRANSFER => true,
- CURLOPT_HEADER => false,
- CURLOPT_POST => true,
- CURLOPT_POSTFIELDS => $post,
- );
- $ch = curl_init($url);
- curl_setopt_array($ch, $options);
- $result = curl_exec($ch);
- curl_close($ch);
- return $result;
- }
- $data = curl_post("http://www.a.com/post/post.php", array('name'=>'caiknife', 'email'=>'caiknife@gmail.com'));
- var_dump($data);
<?php
require dirname(__FILE__).'/function.php'; function curl_post($url, $post) {
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $post,
); $ch = curl_init($url);
curl_setopt_array($ch, $options);
$result = curl_exec($ch);
curl_close($ch);
return $result;
} $data = curl_post("http://www.a.com/post/post.php", array('name'=>'caiknife', 'email'=>'caiknife@gmail.com')); var_dump($data);
最后是用socket来模拟post:
- <?php
- require dirname(__FILE__).'/function.php';
- function socket_post($url, $post) {
- $urls = parse_url($url);
- if (!isset($urls['port'])) {
- $urls['port'] = 80;
- }
- $fp = fsockopen($urls['host'], $urls['port'], $errno, $errstr);
- if (!$fp) {
- echo "$errno, $errstr";
- exit();
- }
- $post = http_build_query($post);
- $length = strlen($post);
- $header = <<<HEADER
- POST {$urls['path']} HTTP/1.1
- Host: {$urls['host']}
- Content-Type: application/x-www-form-urlencoded
- Content-Length: {$length}
- Connection: close
- {$post}
- HEADER;
- fwrite($fp, $header);
- $result = '';
- while (!feof($fp)) {
- // receive the results of the request
- $result .= fread($fp, 512);
- }
- $result = explode("\r\n\r\n", $result, 2);
- return $result[1];
- }
- $data = socket_post("http://www.a.com/post/post.php", array('name'=>'caiknife', 'email'=>'caiknife@gmail.com'));
- var_dump($data);
<?php
require dirname(__FILE__).'/function.php'; function socket_post($url, $post) {
$urls = parse_url($url);
if (!isset($urls['port'])) {
$urls['port'] = 80;
} $fp = fsockopen($urls['host'], $urls['port'], $errno, $errstr);
if (!$fp) {
echo "$errno, $errstr";
exit();
} $post = http_build_query($post);
$length = strlen($post);
$header = <<<HEADER
POST {$urls['path']} HTTP/1.1
Host: {$urls['host']}
Content-Type: application/x-www-form-urlencoded
Content-Length: {$length}
Connection: close {$post}
HEADER; fwrite($fp, $header);
$result = '';
while (!feof($fp)) {
// receive the results of the request
$result .= fread($fp, 512);
}
$result = explode("\r\n\r\n", $result, 2);
return $result[1];
} $data = socket_post("http://www.a.com/post/post.php", array('name'=>'caiknife', 'email'=>'caiknife@gmail.com')); var_dump($data);
这三种方法最后看到的内容都是一样的,但是在是用socket的时候,发送header信息时必须要注意header的完整信息,比如content
type和content length必须要有,connection:
close和post数据之间要空一行,等等;而通过socket取得的内容是包含了header信息的,要处理一下才能获得真正的内容。
使用PHP模拟post提交数据的更多相关文章
- cURL模拟POST提交数据
首先,是这个代码: <?php //curl模拟post提交数据$url = "http://127.0.0.1/immoc/output.php";$post_data = ...
- Fiddler进行模拟Post提交数据,总为null解决方式
Fiddler模拟post提交时总是为空,解决办法 如果是表单提交则要在header加上 ContentType:application/x-www-form-urlencoded 如果是要post提 ...
- php CURL 模拟 POST 提交数据
<?php function liansuo_post($url,$data){ // 模拟提交数据函数 $curl = curl_init(); // 启动一个CURL会话 curl_seto ...
- delphi 模拟POST提交数据
unit GetHttpInfo; interface uses Classes, WinINet, Sysutils, windows, IDURI, IdSSLOpenSSL , IdBaseCo ...
- php模拟post提交数据,用处很多,可用来网站的采集,登陆等等
1. [代码][PHP]代码 <?php //以程序登陆一个论坛登录为例 function bbslogin($user_login, $password, $host, $port = &qu ...
- 三种方法教你如何用PHP模拟post提交数据
php模拟post传值在日常的工作中用到的不是很多,但是在某些特定的场合还是经常用到的. 下面,我整理了三种php模拟post传值的方法,file_get_contents.curl和socket. ...
- 模拟form提交数据
最近在做一个项目,发现ajax不能enctype=”multipart/form-data” 属性的表单,没办法,只能使用form表单直接提交的方法了,但是form表单直接提交会跳转页面,这样很不友好 ...
- Asp.Net模拟post提交数据方法
方法1: System.Net.WebClient WebClientObj = new System.Net.WebClient(); System.Collections.Specialized. ...
- php模拟post提交数据
$data = '{ "id": "17999030", "method": "sayHello", "jso ...
随机推荐
- javascript不同类型数据之间的运算是如何转换的
js中不同类型的基础数据之间可以转换,这种转换是有规则可寻的,并非随意的随机的.在js中有5种基础类型数据:string.number.boolean.null.undefined,其中,常用于计算或 ...
- java模拟http请求上传文件,基于Apache的httpclient
1.依赖 模拟http端的请求需要依赖Apache的httpclient,需要第三方JSON支持,项目中添加 <dependency> <groupId>org.apache& ...
- spring security 一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架
Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架.它提供了一组可以在Spring应用上下文中 配置的Bean,充分利用了Spring ...
- 善用php-fpm的慢执行日志slow log,分析php性能问题
众所周知,mysql有slow query log,根据慢查询日志,我们可以知道那些sql语句有性能问题.作为mysql的好搭档,php也有这样的功能.如果你使用php-fpm来管理php的话,你可以 ...
- bzoj2440 完全平方数 莫比乌斯值+容斥+二分
莫比乌斯值+容斥+二分 /** 题目:bzoj2440 完全平方数 链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2440 题意:求第k个小x数 ...
- node.js和前端js有什么区别
进行前端开发工作需要掌握技能有html. css.js以及各种前端框架,把这些技术玩6就可以成为一名合格的前端开发工作者 而进行nodejs开发,需要掌握js.web服务器原理.关系数据使用, 如果玩 ...
- swt生成、jar可执行包生成.exe可执行文件(giter)
http://tomfish88.iteye.com/blog/1074786 —————————————————————————————————————————————————————————— 最 ...
- .NET开发笔记--对config文件的操作(1)
1先写一些常用的公共类: 在Web.config文件中的配置: <!-- appSettings网站信息配置--> <appSettings> <add key=&quo ...
- C#基础语言知识--编译和执行过程
http://blog.csdn.net/stive_sourcexin/article/details/51329697
- jQery 操作CSS
jQuery操作CSS也是很方便的,咱先看看这几个常用的方法: addClass():向一个元素添加一个或者多个类. removeClass():从一个元素中删除一个类或多个类. toggleClas ...