柳暗花明笔记

    php模拟HTTP的POST或GET请求-响应

    所属分类:[ php&web ] 猫哲学家 发表于:2012-07-25 22:04 浏览:426

    终于搞定模拟HTTP的POST或GET请求-响应,哈哈!
    这个问题弄了几天了,现在终于定。。。
    这是经典的http头格式:

    $request.="POST ".$URL_Info["path"]." HTTP/1.1\n"; $request.="Host: ".$URL_Info["host"]."\n"; $request.="Referer: $referrer\n"; $request.="Content-type: application/x-www-form-urlencoded\n"; $request.="Content-length: ".strlen($data_string)."\n"; $request.="Connection: close\n"; $request.="\n"; $request.=$data_string."\n"; class HttpRequest{ private $method = ''; protected $request_to = ''; private $request_result = ''; protected $data_array = array(); private $new_data_array = ''; private $URL_Info = array(); private $request_info = ''; protected $request_return_result = false; private function print_array($array){ echo ' ';     print_r($array);     echo ' '; } private function my_construct(){ $this->URL_Info = parse_url($this->request_to); } public function set_method($method){ $this->method = $method; } public function set_request_to($url){ $this->request_to = $url; } public function set_data_array($data_array){ $this->my_construct(); $this->data_array = $data_array; $values = array(); foreach ($this->data_array as $key=>$value){ $values[] = "$key=".urlencode($value); } $this->new_data_array = implode("&",$values); } public function set_return_request($set_return_request){ $set_return_request==true?$this->request_return_result=true: $this->request_return_result=false; } public function HttpRequest(){ isset($this->URL_Info['port'])?$this->URL_Info['port']=$this- >URL_Info['port']:$this->URL_Info['port']=80; switch($this->method){ case 'post': // building POST-request: $this->request_info .= "POST ".$this->URL_Info[path]." HTTP/1.1\n"; $this->request_info .= "Host: ".$this->URL_Info["host"]."\n"; $this->request_info .= "Referer: $referrer\n"; $this->request_info .= "Content-type: application/x-www-form-urlencoded\n"; $this->request_info .= "Content-length: ".strlen($this->new_data_array)."\n"; $this->request_info .= "Connection: close\n"; $this->request_info .= "\n"; $this->request_info .= $this->new_data_array."\n"; //$this->print_array($this->request_info); break; case 'get': $getway = $this->URL_Info['path'].'?'.$this->new_data_array; $this->request_info .= "GET ".$getway."HTTP/1.1\n"; $this->request_info .= "Host:".$this->URL_Info['host']."\n"; break; default:; } $fp = fsockopen($this->URL_Info['host'], $this->URL_Info['port']); if(!fp){ ; }else{//send HTTP header fputs($fp, $this->request_info); } if($this->request_return_result == true){ while(!feof($fp)){ //$result send HTTP result $result .= fgets($fp, 1024); $this->request_result = $result; } } fclose($fp); } public function pr_info(){ echo 'the method is: '.$this->method.' '; echo 'the target url is: '.$this->request_to.' '; echo 'the request data is: '; $this->print_array($this->data_array); echo 'the send pattern request data is: '; $this->print_array($this->new_data_array); echo 'the url info data is: '; $this->print_array($this->URL_Info); echo 'the request data info is: '; $this->print_array($this->request_info); echo 'the request result is: '; $this->print_array($this->request_result); } }
    发表于2009年12月14日 17:24 qzone
    标签: php 
    上一编文章 远程linux添加防火墙80端口 下一编文章 rsync从linux到linux的文件同步备份
    最近文章
    发表评论