使用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 ...
随机推荐
- 8237dma的四种传送方式简介
8237A有四种工作方式:单字节传送.数据块传送.请求传送和多片级联. (1)单字节传送(single mode) 单字节传送方式是每次DMA传送时,仅传送一个字节.传送一个字节之后,当前字节计数器减 ...
- C语言 百炼成钢25
/* 题目61:编写一个名为removestring的函数,该函数用于从一个字符串中删除一定量的字符. 该函数接受三个参数: 第1参数代表源字符串 第2参数代表需要删除字符的起始位置(位置从0开始) ...
- 获取一个Assembly中的命名空间列表
通过System.Reflection.Assembly类中提供的方法和属性不能直接获取组件中的命名空间列表.但有方法可以直接获得Assembly中的所有类型,我们便可以通过获取的类型来得到命名空间名 ...
- 【python】pyqt练习
import sys from PyQt4.QtCore import * from PyQt4.QtGui import * import ui_price class PriceDlg(QDial ...
- util.inherits
util.inherits util.inherits(constructor, superConstructor)是一个实现对象间原型继承 的函数. JavaScript 的面向对象特性是基于原型的 ...
- golang build 编译规则
文章来源: http://blog.csdn.net/varding/article/details/12675971 讲述了golang中的条件编译,摘要如下: 第一种条件编译的方法:编译标签 编译 ...
- Python创建数组
1 创建数组 array函数 >>> a=([1,2],[3,4]) >>> array(a) array([[1, 2], [3, 4]]) arange函数: ...
- 基于kubernetes集群的Vitess最佳实践
概要 本文主要说明基于kubernetes集群部署并使用Vitess; 本文假定用户已经具备了kubernetes集群使用环境,如果不具备请先参阅基于minikube的kubernetes集群搭建, ...
- [刷题]ACM ICPC 2016北京赛站网络赛 D - Pick Your Players
Description You are the manager of a small soccer team. After seeing the shameless behavior of your ...
- xshell 没有反应---Xshell按ctrl+s界面无反应的解决办法
在用Xshell管理远程服务器,特别是在用vi编辑配置文件时,总是习惯的用ctrl+s想要保存文件,然后就悲剧了.xsell就再也没有返应只能关了重新打开.但原来修改的文件算是报废了. 在网上搜索了一 ...