php 類比 post 動作 二則

來源:互聯網
上載者:User

1.通過curl函數

 

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

 

2.通過fsockopen

 

$URL=‘http://xxx.xxx.xxx.xx/xx/xxx/top.php';<br />$post_data['clientname'] = "test08";<br />$post_data['clientpasswd'] = "test08";<br />$post_data['submit'] = "ログイン";<br />$referrer="";<br />// parsing the given URL<br />$URL_Info=parse_url($URL);<br />// Building referrer<br />if($referrer=="") // if not given use this script as referrer<br />$referrer=$_SERVER["SCRIPT_URI"];</p><p>// making string from $data<br />foreach($post_data as $key=>$value)<br />$values[]="$key=".urlencode($value);</p><p>$data_string=implode("&",$values);<br />// Find out which port is needed - if not given use standard (=80)<br />if(!isset($URL_Info["port"]))<br />$URL_Info["port"]=80;<br />// building POST-request:<br />$request.="POST ".$URL_Info["path"]." HTTP/1.1/n";<br />$request.="Host: ".$URL_Info["host"]."/n";<br />$request.="Referer: $referrer/n";<br />$request.="Content-type: application/x-www-form-urlencoded/n";<br />$request.="Content-length: ".strlen($data_string)."/n";<br />$request.="Connection: close/n";<br />$request.="/n";<br />$request.=$data_string."/n";<br />$fp = fsockopen($URL_Info["host"],$URL_Info["port"]);<br />fputs($fp, $request);<br />while(!feof($fp)) {<br /> $result .= fgets($fp, 128);<br />}<br />fclose($fp); 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.