手机
当前位置:查字典教程网 >编程开发 >php教程 >php socket方式提交的post详解
php socket方式提交的post详解
摘要:

<?

/*

**POST报文到主机

*/

functionPostToHost($url,$data){

$url=parse_url($url);

if(!$url)return"couldn'tparseurl";

if(!isset($url['port'])){$url['port']="";}

if(!isset($url['query'])){$url['query']="";}

$encoded="";

while(list($k,$v)=each($data)){

$encoded.=($encoded?"&":"");

$encoded.=rawurlencode($k)."=".rawurlencode($v);

}

$port=$url['port']?$url['port']:80;

$fp=fsockopen($url['host'],$port,$errno,$errstr);

if(!$fp)return"Failedtoopensocketto$url[host]$portERROR:$errno-$errstr";

fputs($fp,sprintf("POST%s%s%sHTTP/1.0n",$url['path'],$url['query']?"?":"",$url['query']));

fputs($fp,"Host:$url[host]n");

fputs($fp,"Content-type:application/x-www-form-urlencodedn");

fputs($fp,"Content-length:".strlen($encoded)."n");

fputs($fp,"Connection:closenn");

fputs($fp,"$encodedn");

$line=fgets($fp,1024);

if(!eregi("^HTTP/1..200",$line))return;

$results="";$inheader=1;

while(!feof($fp)){

$line=fgets($fp,1024);

if($inheader&&($line=="n"||$line=="rn")){

$inheader=0;

}

elseif(!$inheader){

$results.=$line;

}

}

fclose($fp);

return$results;

}/*endfunctionPostToHost*/

?>

【php socket方式提交的post详解】相关文章:

phplot生成图片类用法详解

php的控制语句

php以fastCGI的方式运行时文件系统权限问题及解决方法

用 php 编写的日历

PHP与MySQL交互使用详解

php操作memcache缓存方法分享

在php中使用sockets:从新闻组中获取文章

php中file_exists函数使用详解

Apache2的httpd.conf翻译

php解析xml方法实例详解

精品推荐
分类导航