使用PHP模拟post提交数据

分类: PHP LAMP 2013-04-13 12:03 3954人阅读 评论(0) 收藏 举报

这也是个老生常谈的话题了,上午花了点时间把这个问题整理了一下。

一般来说用PHP来模拟post提交数据有三种方法,file_get_contents、curl和socket。

写了个公用函数,专门用来打印post数据:

  1. <?php
  2. function pr() {
  3. $params = func_get_args();
  4. foreach ($params as $key => $value) {
  5. echo "<pre>";
  6. print_r($value);
  7. echo "</pre>";
  8. }
  9. }
<?php
function pr() {
$params = func_get_args();
foreach ($params as $key => $value) {
echo "<pre>";
print_r($value);
echo "</pre>";
}
}

先写一个post.php,用来接收post数据并打印出来:

  1. <?php
  2. require dirname(__FILE__).'/function.php';
  3. if (isset($_POST) && !empty($_POST)) {
  4. pr($_POST);
  5. } else {
  6. pr("NO POST DATA!");
  7. }
<?php
require dirname(__FILE__).'/function.php'; if (isset($_POST) && !empty($_POST)) {
pr($_POST);
} else {
pr("NO POST DATA!");
}

下面是用file_get_contents来模拟post:

  1. <?php
  2. require dirname(__FILE__).'/function.php';
  3. function file_get_contents_post($url, $post) {
  4. $options = array(
  5. 'http' => array(
  6. 'method' => 'POST',
  7. // 'content' => 'name=caiknife&email=caiknife@gmail.com',
  8. 'content' => http_build_query($post),
  9. ),
  10. );
  11. $result = file_get_contents($url, false, stream_context_create($options));
  12. return $result;
  13. }
  14. $data = file_get_contents_post("http://www.a.com/post/post.php", array('name'=>'caiknife', 'email'=>'caiknife@gmail.com'));
  15. 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:

  1. <?php
  2. require dirname(__FILE__).'/function.php';
  3. function curl_post($url, $post) {
  4. $options = array(
  5. CURLOPT_RETURNTRANSFER => true,
  6. CURLOPT_HEADER         => false,
  7. CURLOPT_POST           => true,
  8. CURLOPT_POSTFIELDS     => $post,
  9. );
  10. $ch = curl_init($url);
  11. curl_setopt_array($ch, $options);
  12. $result = curl_exec($ch);
  13. curl_close($ch);
  14. return $result;
  15. }
  16. $data = curl_post("http://www.a.com/post/post.php", array('name'=>'caiknife', 'email'=>'caiknife@gmail.com'));
  17. 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:

  1. <?php
  2. require dirname(__FILE__).'/function.php';
  3. function socket_post($url, $post) {
  4. $urls = parse_url($url);
  5. if (!isset($urls['port'])) {
  6. $urls['port'] = 80;
  7. }
  8. $fp = fsockopen($urls['host'], $urls['port'], $errno, $errstr);
  9. if (!$fp) {
  10. echo "$errno, $errstr";
  11. exit();
  12. }
  13. $post = http_build_query($post);
  14. $length = strlen($post);
  15. $header = <<<HEADER
  16. POST {$urls['path']} HTTP/1.1
  17. Host: {$urls['host']}
  18. Content-Type: application/x-www-form-urlencoded
  19. Content-Length: {$length}
  20. Connection: close
  21. {$post}
  22. HEADER;
  23. fwrite($fp, $header);
  24. $result = '';
  25. while (!feof($fp)) {
  26. // receive the results of the request
  27. $result .= fread($fp, 512);
  28. }
  29. $result = explode("\r\n\r\n", $result, 2);
  30. return $result[1];
  31. }
  32. $data = socket_post("http://www.a.com/post/post.php", array('name'=>'caiknife', 'email'=>'caiknife@gmail.com'));
  33. 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提交数据的更多相关文章

  1. cURL模拟POST提交数据

    首先,是这个代码: <?php //curl模拟post提交数据$url = "http://127.0.0.1/immoc/output.php";$post_data = ...

  2. Fiddler进行模拟Post提交数据,总为null解决方式

    Fiddler模拟post提交时总是为空,解决办法 如果是表单提交则要在header加上 ContentType:application/x-www-form-urlencoded 如果是要post提 ...

  3. php CURL 模拟 POST 提交数据

    <?php function liansuo_post($url,$data){ // 模拟提交数据函数 $curl = curl_init(); // 启动一个CURL会话 curl_seto ...

  4. delphi 模拟POST提交数据

    unit GetHttpInfo; interface uses Classes, WinINet, Sysutils, windows, IDURI, IdSSLOpenSSL , IdBaseCo ...

  5. php模拟post提交数据,用处很多,可用来网站的采集,登陆等等

    1. [代码][PHP]代码 <?php //以程序登陆一个论坛登录为例 function bbslogin($user_login, $password, $host, $port = &qu ...

  6. 三种方法教你如何用PHP模拟post提交数据

    php模拟post传值在日常的工作中用到的不是很多,但是在某些特定的场合还是经常用到的. 下面,我整理了三种php模拟post传值的方法,file_get_contents.curl和socket. ...

  7. 模拟form提交数据

    最近在做一个项目,发现ajax不能enctype=”multipart/form-data” 属性的表单,没办法,只能使用form表单直接提交的方法了,但是form表单直接提交会跳转页面,这样很不友好 ...

  8. Asp.Net模拟post提交数据方法

    方法1: System.Net.WebClient WebClientObj = new System.Net.WebClient(); System.Collections.Specialized. ...

  9. php模拟post提交数据

    $data = '{ "id": "17999030", "method": "sayHello", "jso ...

随机推荐

  1. python判断字符串是否为空的方法s.strip()=='' if not s.strip():

    python 判断字符串是否为空用什么方法? 复制代码 s=' ' if s.strip()=='':     print 's is null' 或者 if not s.strip():     p ...

  2. oracle tuning 工具

    工欲善其事, 必先利其器. oracle 调优方面有很多工具, 目前 UI 个人只打算使用 Toad. 重要文件 一. alert log file. (位置 parameter BACKGROUND ...

  3. kvm初体验之四:从Host登录Guest的五种方式

    1. virt-viewer virt-viewer -c qemu:///system vm1 2. virt-manager (以非root身份运行) virt-manager -c qemu:/ ...

  4. Sql server不同数据类型间拼接(+)

    )+'m' 输出 4m 若 +'m' 输出:在将 varchar 值 'm' 转换成数据类型 int 时失败.  

  5. 【VBA】合并多个excel文件

    From http://www.zhihu.com/question/20366713 VBA代码如下: Sub 工作薄间工作表合并() Dim FileOpen Dim X As Integer A ...

  6. JavaScript------如何查看var变量是否是指定类型

    function isArray(a) { //Date,Array,String,Object,Function,Boolean,Number return a.constructor.toStri ...

  7. Android 热修复 Tinker接入及源代码浅析

    本文已在我的公众号hongyangAndroid首发.转载请标明出处: http://blog.csdn.net/lmj623565791/article/details/54882693本文出自张鸿 ...

  8. iOS-tableView本地动画刷新

    比如:就拿删除tableView中一个Cell为例子. // XXXTableViewCellDelegate - (void)tapDeleteHelloUser:(CJHelloTableView ...

  9. 认识tornado(二)

    前面我们对 Tornado 自带的 hello world 作了代码组织上的解释,但是没有更加深入细致地解释.这里我们直接从main()函数开始,单步跟随,看看tornado都干了些什么. 下面是 m ...

  10. JavaWeb开发的一系列可能的配置

    最近电脑硬盘坏了Orz...,重装了.唉!软件什么的都要重新装,重新设置,好麻烦! 我决定写下重装javaweb开发环境的过程 1.Java的安装,配置 https://www.runoob.com/ ...