php 模拟POST提交的2种方法详解

yipeiwu_com5年前PHP代码库

一、通过curl函数

复制代码 代码如下:

$post_data = array();
$post_data['clientname'] = "test08";
$post_data['clientpasswd'] = "test08";
$post_data['submit'] = "submit";
$url='http://xxx.xxx.xxx.xx/xx/xxx/top.php';
$o="";
foreach ($post_data as $k=>$v)
{
    $o.= "$k=".urlencode($v)."&";
}
$post_data=substr($o,0,-1);
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL,$url);
//为了支持cookie
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$result = curl_exec($ch);

二、通过fsockopen
复制代码 代码如下:

$URL=‘http://xxx.xxx.xxx.xx/xx/xxx/top.php';
$post_data['clientname'] = "test08";
$post_data['clientpasswd'] = "test08";
$post_data['submit'] = "ログイン";
$referrer="";
// parsing the given URL
$URL_Info=parse_url($URL);
// Building referrer
if($referrer=="") // if not given use this script as referrer
$referrer=<PRE class=php name="code">{1}</PRE><BR>
SERVER["SCRIPT_URI"]; // making string from $dataforeach($post_data as $key=>$value)$values[]="$key=".urlencode($value); $data_string=implode("&",$values);// Find out which port is needed - if not given use standard (=80)if(!isset($URL_Info["port"]))$URL_Info["port"]=80;//
 building POST-request:$request.="POST ".$URL_Info["path"]." HTTP/1.1\n";$request.="Host: ".$URL_Info["host"]."\n";$request.="Referer: $referrer\n";$request.="Content-type: application/x-www-form-urlencoded\n";$request.="Content-length: ".strlen($data_string)."\n";$request.="Connection:
 close\n";$request.="\n";$request.=$data_string."\n";$fp = fsockopen($URL_Info["host"],$URL_Info["port"]);fputs($fp, $request);while(!feof($fp)) { $result .= fgets($fp, 128);}fclose($fp);
<PRE></PRE>
<P><BR>
 </P>
<P>Snoopy 类(2)<BR>
sourceforge.net/projects/snoopy/</P>
<P>http://www.redalt.com/xref/trunk/nav.htm?wp-includes/class-snoopy.php.htm</P>
<P>HTTP类(1,2)</P>
<P>http://www.phpclasses.org/browse/download/1/file/5/name/http.php</P>
<P>PEAR HTTP_Request</P>
<P>http://pear.php.net/package/HTTP_Request</P>
<P class=akpc_pop>Popularity: 70%</P>
<P> </P>

相关文章

php查询内存信息操作示例

本文实例讲述了php查询内存信息操作。分享给大家供大家参考,具体如下: php查询内存信息,是为了更好的查看内存使用情况,更好的优化代码。 查看当前内存使用情况使用:memory_get...

详解PHP序列化反序列化的方法

经常看到一些配置文件里面存放的是一些类似带有格式的变量名称和值,其实就是一个序列化的过程,在需要用到这些数据库的时候会进行一个反序列化过程,就是将这个字符串再还原成他原来的数据结构。下面...

PHP进行批量任务处理不超时的解决方法

本文实例分析了PHP进行批量任务处理不超时的解决方法。分享给大家供大家参考,具体如下: PHP批量任务处理 PHP在批量处理任务的时候会超时,其实解决方法很简单了,就是把任务分割,一次处...

php防盗链的常用方法小结

1.简单防盗链 复制代码 代码如下: $ADMIN[defaulturl] = "http://jb51.net/404.htm";//盗链返回的地址 $okaysites = arra...

php实现留言板功能

php实现留言板功能

这个小小的留言板功能适合班级内或者公司内部之间的讨论,对话和留言,非常的方便,更重要的是无需网络,对于公司管理层来说是非常乐于常见的, 下面是这个留言板的写法: 1 首先是登录页面:...