189 8069 5689

fsockopen封装

/**

成都创新互联公司服务项目包括鹤城网站建设、鹤城网站制作、鹤城网页制作以及鹤城网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,鹤城网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到鹤城省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!

* post数据

* @param string $url post的url

* @param int $limit 返回的数据的长度

* @param string $post post数据,字符串形式username=\'dalarge\'&password=\'123456\'

* @param string $cookie 模拟 cookie,字符串形式username=\'dalarge\'&password=\'123456\'

* @param string $ip ip地址

* @param int $timeout 连接超时时间

* @param bool $block 是否为阻塞模式

* @return string 返回字符串

*/

private function _ps_post($url, $limit = 0, $post = \'\', $cookie = \'\', $ip = \'\', $timeout = 15, $block = true) {

$return = \'\';

$matches = parse_url($url);

$host = $matches[\'host\'];

$path = $matches[\'path\'] ? $matches[\'path\'].($matches[\'query\'] ? \'?\'.$matches[\'query\'] : \'\') : \'/\';

$port = !empty($matches[\'port\']) ? $matches[\'port\'] : 80;

$siteurl = $this->_get_url();

if($post) {

$out = "POST $path HTTP/1.1rn";

$out .= "Accept: */*rn";

$out .= "Referer: ".$siteurl."rn";

$out .= "Accept-Language: zh-cnrn";

$out .= "Content-Type: application/x-www-form-urlencodedrn";

$out .= "User-Agent: $_SERVER[HTTP_USER_AGENT]rn";

$out .= "Host: $hostrn" ;

$out .= \'Content-Length: \'.strlen($post)."rn" ;

$out .= "Connection: Closern" ;

$out .= "Cache-Control: no-cachern" ;

$out .= "Cookie: $cookiernrn" ;

$out .= $post ;

} else {

$out = "GET $path HTTP/1.1rn";

$out .= "Accept: */*rn";

$out .= "Referer: ".$siteurl."rn";

$out .= "Accept-Language: zh-cnrn";

$out .= "User-Agent: $_SERVER[HTTP_USER_AGENT]rn";

$out .= "Host: $hostrn";

$out .= "Connection: Closern";

$out .= "Cookie: $cookiernrn";

}

$fp = @fsockopen(($ip ? $ip : $host), $port, $errno, $errstr, $timeout);

if(!$fp) return \'\';

stream_set_blocking($fp, $block);

stream_set_timeout($fp, $timeout);

@fwrite($fp, $out);

$status = stream_get_meta_data($fp);

if($status[\'timed_out\']) return \'\';

while (!feof($fp)) {

if(($header = @fgets($fp)) && ($header == "rn" || $header == "n")) break;

}

$stop = false;

while(!feof($fp) && !$stop) {

$data = fread($fp, ($limit == 0 || $limit > 8192 ? 8192 : $limit));

$return .= $data;

if($limit) {

$limit -= strlen($data);

$stop = $limit <= 0;

}

}

@fclose($fp);

//部分虚拟主机返回数值有误,暂不确定原因,过滤返回数据格式

$return_arr = explode("n", $return);

if(isset($return_arr[1])) {

$return = trim($return_arr[1]);

}

unset($return_arr);

return $return;

}


当前标题:fsockopen封装
当前地址:http://cdxtjz.cn/article/cjcijs.html

其他资讯