欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-04-07 18:07:45
一、通过curl函数复制代码 代码如下:$post_data = array();$post_data['clientname'] = "test08";$post_dat...

一、通过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>