欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

使用PHP模拟post提交数据 有大用

shiping1 的头像

使用PHP模拟post提交数据

分类: PHP LAMP 1029人阅读 评论(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. }  


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

  2. require dirname(__FILE__).'/function.php';  

  3.   

  4. if (isset($_POST) && !empty($_POST)) {  

  5.     pr($_POST);  

  6. else {  

  7.     pr("NO POST DATA!");  

  8. }  


下面是用file_get_contents来模拟post:
  1. <?php  

  2. require dirname(__FILE__).'/function.php';  

  3.   

  4. function file_get_contents_post($url$post) {  

  5.     $options = array(  

  6.         'http' => array(  

  7.             'method' => 'POST',  

  8.             // 'content' => 'name=caiknife&email=caiknife@gmail.com',  

  9.             'content' => http_build_query($post),  

  10.         ),  

  11.     );  

  12.   

  13.     $result = file_get_contents($url, false, stream_context_create($options));  

  14.   

  15.     return $result;  

  16. }  

  17.   

  18. $data = file_get_contents_post("http://www.a.com/post/post.php"array('name'=>'caiknife''email'=>'caiknife@gmail.com'));  

  19.   

  20. var_dump($data);  


很简单是吧?再来看看curl模拟post:
  1. <?php  

  2. require dirname(__FILE__).'/function.php';  

  3.   

  4. function curl_post($url$post) {  

  5.     $options = array(  

  6.         CURLOPT_RETURNTRANSFER => true,  

  7.         CURLOPT_HEADER         => false,  

  8.         CURLOPT_POST           => true,  

  9.         CURLOPT_POSTFIELDS     => $post,  

  10.     );  

  11.   

  12.     $ch = curl_init($url);  

  13.     curl_setopt_array($ch$options);  

  14.     $result = curl_exec($ch);  

  15.     curl_close($ch);  

  16.     return $result;  

  17. }  

  18.   

  19. $data = curl_post("http://www.a.com/post/post.php"array('name'=>'caiknife''email'=>'caiknife@gmail.com'));  

  20.   

  21. var_dump($data);  


最后是用socket来模拟post:
  1. <?php  

  2. require dirname(__FILE__).'/function.php';  

  3.   

  4. function socket_post($url$post) {  

  5.     $urls = parse_url($url);  

  6.     if (!isset($urls['port'])) {  

  7.         $urls['port'] = 80;  

  8.     }  

  9.   

  10.     $fp = fsockopen($urls['host'], $urls['port'], $errno$errstr);  

  11.     if (!$fp) {  

  12.         echo "$errno, $errstr";  

  13.         exit();  

  14.     }  

  15.   

  16.     $post = http_build_query($post);  

  17.     $length = strlen($post);  

  18.     $header = <<<HEADER  

  19. POST {$urls['path']} HTTP/1.1  

  20. Host: {$urls['host']}  

  21. Content-Type: application/x-www-form-urlencoded  

  22. Content-Length: {$length}  

  23. Connection: close  

  24.   

  25. {$post}  

  26. HEADER;  

  27.   

  28.     fwrite($fp$header);  

  29.     $result = '';  

  30.     while (!feof($fp)) {  

  31.         // receive the results of the request  

  32.         $result .= fread($fp, 512);  

  33.     }  

  34.     $result = explode("\r\n\r\n"$result, 2);  

  35.     return $result[1];  

  36. }  

  37.   

  38. $data = socket_post("http://www.a.com/post/post.php"array('name'=>'caiknife''email'=>'caiknife@gmail.com'));  

  39.   

  40. var_dump($data);  


这三种方法最后看到的内容都是一样的,但是在是用socket的时候,发送header信息时必须要注意header的完整信息,比如content type和content length必须要有,connection: close和post数据之间要空一行,等等;而通过socket取得的内容是包含了header信息的,要处理一下才能获得真正的内容。


来自 http://blog.csdn.net/caiknife/article/details/8796505



PHP的三种HTTP请求,php模拟post 提交

tag: HTTP请求,post 提交

http://www.jb51.net/article/21092.htm

php 接口类与抽象类的实际作用

http://www.189works.com/article-44046-1.html

php模拟post行为代码总结(POST方式不是绝对安全)

---------------------------------

http://hi.baidu.com/stodbx2002/item/749b7e102a503d6d70d5e8ea

PHP的三种HTTP请求,php模拟post 提交

PHP的三种HTTP请求,php模拟post 提交

方法一:利用php的socket编程来直接给接口发送数据来模拟post的操作。

建立两个文件post.php,getpost.php

post.php内容如下:

<!--?php

 $flag = 0;

 $post = '''';

 $errno = '''';

 $errstr = '''';

 //要post的数据

$argv = array(

    ''var1''=>''abc'',

    ''var2''=>''how are you , my friend??''

);

//构造要post的字符串

foreach ($argv as $key=>$value) {

    if ($flag!=0) {

        $post .= "&";

        $flag = 1;

    }

    $post.= $key."="; $post.= urlencode($value);

    $flag = 1;

    }

    $length = strlen($post);

     //创建socket连接

   $fp = fsockopen("localhost",81,$errno,$errstr,10) or exit($errstr."--->".$errno);

    //构造post请求的头

    $header  = "POST /flandy/getpost.php HTTP/1.1\r\n";

    $header .= "Host:127.0.0.1\r\n";

    $header .= "Referer:/flandy/post.php\r\n";

    $header .= "Content-Type: application/x-www-form-urlencoded\r\n";

    $header .= "Content-Length: ".$length."\r\n";

    $header .= "Connection: Close\r\n\r\n";

    //添加post的字符串

    $header .= $post."\r\n";

 

    //发送post的数据

   fputs($fp,$header);

    $inheader = 1;

    while (!feof($fp)) {

        $line = fgets($fp,1024); //去除请求包的头只显示页面的返回数据

        if ($inheader && ($line == "\n" || $line == "\r\n")) {

             $inheader = 0;

        }

        if ($inheader == 0) {

          echo $line;

        }

    }

fclose($fp);

?>

getpost.php的内容如下

<!--?php

echo "this is the data posted";

echo "

";

 

print_r($_REQUEST);

echo "

";

 

?>

结果输出:

this is the data posted

Array(

[var1] => abc

[var2] => how are you , my friend??

)

方法二:

使用PHP的curl扩展或HttpClient.class.php类,这两个非常类似,下面简单的列出curl的实现代码。

两个文件post2.php和getpost2.php

post2.php的内容如下:

<!--?php

$psecode = ’NDE005’;

$website = ’www.baidu.com’;

$amt = 1;

$pwd = 123456;

$ch = curl_init();

$curl_url = "http://localhost:81/flandy/getpost2.php?web=" . $website .

"&pwd=" . $pwd . "&action=check&pseid=" . $psecode .

"&amt=" . $amt;

curl_setopt($ch, CURLOPT_URL, $curl_url);

curl_setopt($ch, CURLOPT_POST, 1);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//不直接输出,返回到变量

$curl_result = curl_exec($ch);

$result = explode('','', $curl_result);

curl_close($ch);

print_r($result);

?>

getpost2.php的内容如下:

<!--?php

echo "returndata
";

echo "

";

 

print_r($_REQUEST);

echo "

";

 

?>

结果输出:

Array (

  [0] => returndata  Array(

    [web] => ’wwwbaiducom’

    [pwd] => 123456

    [action] => check

    [pseid] => ’NDE005’

    [amt] => 

 )

)

方法三:这个要借助第三方类库HttpClient可以到这里下载:http://scripts.incutio.com/httpclient/<?php

require_once ''HttpClient.class.php’;

$params = array(’web’ => ’www.baidu.com’,

’pwd’ => ’123456’,

’action’ => ’check’,

’pseid’ => ’NDE005’,

’amt’ => 1);

$pageContents = HttpClient::quickPost(’http://localhost:81/flandy/getpost3.php’, $params);

$result = explode(’,’, $pageContents);

print_r($result);

?>

补充:

HTTP请求常用方案有以下(友情提示:排列顺序只为所想起时的先后顺序并再无特别含义)

1.HttpClient 

Version 0.9, Simon Willison April 6th 2003

http://scripts.incutio.com/httpclient/HttpClient.class.php

2.snoopy

Snoopy Snoopy 1.2.3 November 7, 2005 

http://snoopy.sourceforge.net/

3.pear::http_client

1.1.0 (stable) was released on 2006-06-03

http://pear.php.net/package/HTTP_Client

4.curl or php_curl

5.wget

6.php_socket

前3个算是比较完整的类,所以后面的暂时不考虑了。

库的选择一般原则是找用的人多,更新持久的,因此 pear::http_client 一马当先,但这个必须是统筹在PEAR之下,因为要用到一些PEAR的辅助类,不是很适合单独使用,请回去等录用通知吧。这回合Snoopy 领先一步,但粗略一看核心文件Snoopy.class.php 体重38KB,再看 HttpClient 感觉是相当苗条了,核心文件 HttpClient.class.php 占地12KB,这回合 HttpClient 也得一分,不过最后更新日期让人看得心寒。

人气测试(pear::http_client友情出场):

1.Google Trends 

结果:放弃。

因为 Snoopy 在某个世界实在太有名气了,而且"http client" 关键字也太含糊。

2.Google Code Search

规则:php + 包含类名的一行并用双引号括起来

HttpClient 100

http://www.google.com/codesearch ... class+HttpClient"

Snoopy 100

http://www.google.com/codesearch ... +"class+Snoopy"

pear::http_client 12 (还是请继续回去等通知吧)

http://www.google.com/codesearch ... "&btnG=Search

核心PK:

一般来说,php 的HTTP CLIENT都是通过PHP_CURL或者PHP_SOCKET来实现的,所以这局应该又是平手。

功能PK:

一般来说功能与体重成正比,所以 Snoopy 的给人的第一印象还是很令人得期待的。(考察未完成)


-->

来自 http://txjia.com/tip/?2012-CD-F4TP
来自 http://www.360doc.com/content/11/1214/13/706976_172174792.shtml
普通分类: