function getsock($strUrl,$arrParameter=array(),$strMethod="POST"){
if(!$strUrl||!$strMethod){ return "";}
$strMethod = strtoupper($strMethod);
$arrUrl = parse_url($strUrl);
if($arrUrl["port"]==0){$arrUrl["port"]=80;}
$fso = fsockopen($arrUrl["host"],$arrUrl["port"],$intErrno,$strError,10);
if(!$fso){
return "";
}else{
$strFileName = $arrUrl["path"];
if($arrUrl["query"]!=""){
$strFileName.= "?".$arrUrl["query"];
}
$strHeader = "$strMethod $strFileName HTTP/1.1/r/n";
$strHeader .= "Host:".$arrUrl["host"].":".$arrUrl["port"]."/r/n";
if($strMethod=="POST"){
if(is_array($arrParameter)){
$blnFlag = 0;
foreach($arrParameter as $strKey=>$Value){
if($blnFlag){
$strParameter.= "&";
$blnFlag = 1;
}
$strParameter.= $strKey."=";
$strParameter.= urlencode($Value);
$blnFlag = 1;
}
}
$intLength = strlen($strParameter);
$strHeader .= "Content-Type: application/x-www-form-urlencoded/r/n";
$strHeader .= "Content-Length: ".$intLength."/r/n";
}
$strHeader .= "Connection: Close/r/n/r/n";
if($strParameter!=""){
$strHeader .= $strParameter."/r/n";
}
fputs($fso,$strHeader);
$strResult = "";
$intHeader = 1;
while(!feof($fso)) {
$strLine = fgets($fso,1024);
//去除请求包的头只显示页面的返回数据
if ($intHeader && ($strLine == "/n" || $strLine == "/r/n")) {
$intHeader = 0;
}
if ($intHeader == 0) {
$strResult .= $strLine;
}
}
fclose($fso);
return $strResult;
}
}
getsock函数的使用方法(默认是POST)
例子:
getsock('http://www.xxx.net/login.php',array('username'=>'admin','password'=>'123456'));