• socket编程发送GET请求


    可以根据几根url地址,分析出主机,地址,协议等,然后用封装成的类拼接成GET请求信息,用fsockopen连接主机,进行读取操作,获取响应信息,打印

    <?php
    //http连接接口
    interface proto{
        //连接url
        public function conn($url);
        //发送get请求
        public function get();
        //发送post
        public function post($num);
        //关闭连接
        public function close();
    }
    class Http implements proto{
        const CRLF="
    ";
        protected $response='';
        protected $poststr='';
        protected $errno=-1;
        protected $errstr='';
        protected $version='HTTP/1.1';
        protected $fh=null;
        protected $url=array();
        protected $line=array();
        protected $header=array();
        protected $body=array();
    
        public function __construct($url){
            $this->conn($url);
            $this->setheader();
        }
        //负责写请求行
        protected function setLine($method){
            $this->line[0]=$method .' '.$this->url['path'] .' '.$this->version;
        }
        //负责写请求信息
        protected function setHeader(){
            $this->header[]='Host:'.$this->url['host'];
        }
        //负责写主题信息
        protected function setBody($body){
            $this->body=array(http_build_query($body));
        }
    
        //连接url
        public function conn($url){
            $this->url=parse_url($url);
            //判断端口
            if(!isset($this->url['port'])){
                $this->url['port']=80;
            }
            $this->fh=fsockopen($this->url['host'],$this->url['port'],$this->errno,$this->errstr,3);
        }
        //构造get请求数据
        public function get(){
            $this->setLine('GET');
            $this->request();
            return $this->response;
        }
        //构造post请求数据
        public function post($body=array()){
            $this->setLine('POST');
            
            $this->header[]='content-type:application/x-www-form-urlencoded';
            // print_r($this->header);
            $this->setBody($body);
            $this->header[]='content-length:'.strlen($this->body[0]);
            $this->request();
            return $this->response;
        }
        
        //真正的请求
        public function request(){
            $req=array_merge($this->line,$this->header,array(''),$this->body,array(''));//拼接请求信息为数组
            // print_r($req);
            $req=implode(self::CRLF,$req);
            //echo $req;exit();
            fwrite($this->fh,$req);
    
            while(!feof($this->fh)){
                $this->response.=fread($this->fh,1024);
            }
            $this->close();
            
        }
        //关闭连接
        public function close(){
            fclose($this->fh);
        }
    
    }
    
    //调试
    $url='http://localhost/tieba/cunchu.php';
    $http=new Http($url);
    // echo $http->get();
    // print_r($http);
    echo $http->post(array('username'=>'zhangsan','title'=>'试试发帖','content'=>'这是用http协议拼接数据发送的'));
    ?>
    
     
  • 相关阅读:
    其他:Oracle并购sun之后的影响之我见
    60款很酷的 jQuery 幻灯片演示和下载
    分享35个高质量的 Apple 风格图标素材
    25个漂亮的旅游网站设计作品欣赏
    tomcat内存溢出总结
    一个非常强大完整的web表单验证程序
    java socket (回顾)
    jdbc 公共类(2)
    各大IT公司笔试真题汇总
    软件公司的岗位职责
  • 原文地址:https://www.cnblogs.com/lzzhuany/p/4842159.html
Copyright © 2020-2023  润新知