使用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 ...
随机推荐
- python判断字符串是否为空的方法s.strip()=='' if not s.strip():
python 判断字符串是否为空用什么方法? 复制代码 s=' ' if s.strip()=='': print 's is null' 或者 if not s.strip(): p ...
- oracle tuning 工具
工欲善其事, 必先利其器. oracle 调优方面有很多工具, 目前 UI 个人只打算使用 Toad. 重要文件 一. alert log file. (位置 parameter BACKGROUND ...
- kvm初体验之四:从Host登录Guest的五种方式
1. virt-viewer virt-viewer -c qemu:///system vm1 2. virt-manager (以非root身份运行) virt-manager -c qemu:/ ...
- Sql server不同数据类型间拼接(+)
)+'m' 输出 4m 若 +'m' 输出:在将 varchar 值 'm' 转换成数据类型 int 时失败.
- 【VBA】合并多个excel文件
From http://www.zhihu.com/question/20366713 VBA代码如下: Sub 工作薄间工作表合并() Dim FileOpen Dim X As Integer A ...
- JavaScript------如何查看var变量是否是指定类型
function isArray(a) { //Date,Array,String,Object,Function,Boolean,Number return a.constructor.toStri ...
- Android 热修复 Tinker接入及源代码浅析
本文已在我的公众号hongyangAndroid首发.转载请标明出处: http://blog.csdn.net/lmj623565791/article/details/54882693本文出自张鸿 ...
- iOS-tableView本地动画刷新
比如:就拿删除tableView中一个Cell为例子. // XXXTableViewCellDelegate - (void)tapDeleteHelloUser:(CJHelloTableView ...
- 认识tornado(二)
前面我们对 Tornado 自带的 hello world 作了代码组织上的解释,但是没有更加深入细致地解释.这里我们直接从main()函数开始,单步跟随,看看tornado都干了些什么. 下面是 m ...
- JavaWeb开发的一系列可能的配置
最近电脑硬盘坏了Orz...,重装了.唉!软件什么的都要重新装,重新设置,好麻烦! 我决定写下重装javaweb开发环境的过程 1.Java的安装,配置 https://www.runoob.com/ ...