PHP fsockopen模拟发送post set请求

class Http {
	public $lineAndHeader;
	public $path;
	public $method;
	public $port = 80;
	public $version = 'HTTP/1.1';
	public function __construct($url)
	{
		$this->path=parse_url($url);
		$this->setLine();
	}	
	//设置请求行
	public function setLine()
	{
		if(!isset($this->path['query'])){
			$this->path['query'] = null;
		}
		return array($this->method.' '.$this->path['path'].'?'.$this->path['query'].' '.$this->version);
	}
	//设置头信息
	public function setHeader($header = array())
	{
		if(isset($this->path['port'])){
			$this->port = $this->path['port'];
		}
		$commonHeader = array('Host: '.$this->path['host'].':'.$this->port);
		return array_merge($commonHeader,$header);
	}
	//GET请求
	public function get()
	{
		$this->method = 'GET';
		$this->lineAndHeader = array_merge($this->setLine(),$this->setHeader(),array(''),array(''));
	}
	//POST请求
	public function post($data)
	{
		$this->method='POST';
		$this->lineAndHeader = array_merge($this->setLine(),$this->setHeader(array('Content-type: application/x-www-form-urlencoded','Content-length: '.strlen($data))),array(''),array($data));
	}
	//发送请求
	public function request()
	{
		$str = implode("\r\n",$this->lineAndHeader);
		//echo $str;exit;
		$fp = fsockopen($this->path['host'],$this->port,$errno,$errstr,60);
		fwrite($fp, $str,strlen($str));
		$res = '';
		while(!feof($fp)){
			$res .= fgets($fp, 512);
		}
		fclose($fp);
		return $res;
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值