使用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. Using a long as ArrayList index in java

    http://stackoverflow.com/questions/459643/using-a-long-as-arraylist-index-in-java http://bbs.csdn.ne ...

  2. .NET开发相关使用工具和框架

    转自: http://www.cnblogs.com/NatureSex/archive/2011/04/21/2023265.html 开发类 visual_studio 2005-2010系列-- ...

  3. The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Cha

    The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Cha ...

  4. mysql DBA 指南

    Mysql目录 数据库介绍.常见分类 Mysql入门 Mysql安装配置 Mysql多实例安装配置 Mysql常用基本命令 Mysql权限体系 Mysql数据库备份和恢复 Mysql日志 Mysql逻 ...

  5. python入门(一):基础语法

    1.修改字符编码# -*- coding: cp-1252 -*-2.标识符以字母或下划线开头,大小写敏感3.以缩进表示代码块,同一个代码块缩进必须一致4.多行代码用反斜杠表示,() [] {}则不需 ...

  6. MySQL设置密码的三种方法

    其设置密码有三种方法: a. ./mysqladmin -u root -p oldpassword newpasswd(记住这个命令是在/usr/local/mysql/bin中外部命令) b. S ...

  7. SQL Server 2008 R2 开启远程连接

    因为sql server 2008默认是不允许远程连接的,sa帐户也是默认禁用的,如果想要在本地用SSMS(SQL Server Management Studio Express) 连接远程服务器上 ...

  8. 【MVC model 验证失效 】【Unexpected token u in JSON at position 0】【jquery-plugin-validation】

    问题描述:mvc model 调用jquery-plugin-validation 实现 前台的数据验证,时报错 Unexpected token u in JSON at position 0 很讨 ...

  9. HTML中条件注释的高级应用

    在页面头部加入 <!--[if lt IE 9]><html class="ie"><![endif]--> 可简单CSS Hack,IE6.I ...

  10. 巨蟒python全栈开发-第8天 文件操作

    一.文件操作 今日大纲: 1.文件操作->open() open 打开 f=open(文件路径,mode='模式',encoding='编码格式') #python最最底层操作的就是bytes ...