<?php $srv_ip = '192.168.1.5';//你的目標服務地址. $srv_port = 80;//連接埠 $url = 'http://localhost/fsock.php'; //接收你post的URL具體地址 $fp = ''; $errno = 0;//錯誤處理 $errstr = '';//錯誤處理 $timeout = 10;//多久沒有連上就中斷 $post_str = "username=demo&password=hahaha";//要提交的內容. //開啟網路的 Socket 連結。 $fp = fsockopen($srv_ip,$srv_port,$errno,$errstr,$timeout); if (!$fp){ echo('fp fail'); } $content_length = strlen($post_str); $post_header = "POST $url HTTP/1.1rn"; $post_header .= "Content-Type: application/x-www-form-urlencodedrn"; $post_header .= "User-Agent: MSIErn"; $post_header .= "Host: ".$srv_ip."rn"; $post_header .= "Content-Length: ".$content_length."rn"; $post_header .= "Connection: closernrn"; $post_header .= $post_str."rnrn"; fwrite($fp,$post_header); $inheader = 1; while(!feof($fp)){//測試檔案指標是否到了檔案結束的位置 $line = fgets($fp,1024); //去掉請求包的頭資訊 if ($inheader && ($line == "n" || $line == "rn")) { $inheader = 0; } if ($inheader == 0) { echo $line; } } fclose($fp); unset ($line); ?> |