• PHP实现HTTP的POST与GET 类


    PHP实现HTTP的POST与GET 类

    2015-01-25

    http.class.php代码:

    <?php

    /*PHP + socket 编程,发送HTTP请求

    * 要求能模拟下载,注册,登陆,批量发站

    * */

    //http 请求类的接口

    interfaceProto{

        //连接url

        publicfunction conn($url);

        //发送get查询

        publicfunction get();

        //发送post查询

        publicfunction post();

        //关闭连接

        publicfunction close();

    }

    classHttpimplementsProto{

        protected$url = array();

        protected$version = 'HTTP/1.1';    //请求协议

        protected$fh =null;        //连接信息

        constCRLF = " ";        // carriage return line feed;

        protected$errno = -1;        //错误号

        protected$errstr = '';

        protected$response = '';

        protected$line = array();     //请求行

        protected$header = array();    //头信息

        protected$body = array();    //主体信息

        publicfunction __construct($url){

            $this->conn($url);

            $this->setHeader("Host:".$this->url['host']);

        }

        

        //此方法负责写请求行

        protectedfunction setLine($method){

            $this->line[0] =$method.' '.$this->url['path'].'? '.$this->url['query'].' '.$this->version;    

        }

        //此方法负责写头信息

        publicfunction setHeader($headerline){

            $this->header[] =$headerline;

        }

        //此方法负责写主体信息

        protectedfunction setBody($body){    

            $this->body[] =http_build_query($body);//将数组构建成HTTP请求字符串

            //print_r($this->body);

        }

        

        //连接url

        publicfunction conn($url){

            $this->url =parse_url($url);

            if(!isset($this->url['port'])){

                $this->url['port'] = 80;

            }

            if(!isset($this->url['query'])){

                $this->url['query'] = '';

            }

            //print_r($this->url);

            $this->fh =fsockopen($this->url['host'],$this->url['port'],$this->errno,$this->errstr,3);    //连接主机,错误号,错误字符串,超时时间:3s

        }

        //构造get查询的数据

        publicfunction get(){

            $this->setLine('GET');

            $this->request();

            return$this->response;

        }

        //构造post查询的数据

        publicfunction post($body = array()){

            $this->setLine('POST');    //设置请求行

            $this->setHeader("Content-type:application/x-www-form-urlencoded");//设置content-type

            $this->setBody($body);//设计主体信息,比GET不一样

            $this->setHeader('Content-length:'.strlen($this->body[0]));//计算content-length

            $this->request();

            return$this->response;

        }

        // 真正请求

        publicfunction request(){

            //把头信息,实体信息,拼接起来

            //$req = array_merge($this->line,$this->header,array(''), array(http_build_query($body)),array(''));

            $req =array_merge($this->line,$this->header,array(''),$this->body,array(''));

            //print_r($req);

            $req =implode(self::CRLF,$req);

            echo "请求行信息:<br/>",$req;        //拼接头信息

            fwrite($this->fh,$req);

            //print_r($this->fh);

            $this->response .= "<br/><br/>返回行信息:<br/>";

            while(!feof($this->fh)){

                $this->response .=fread($this->fh,1024);

            }

            $this->close();        //关闭连接

        }

        //关闭连接

        publicfunction close(){

            fclose($this->fh);

        }

    }

    /*

    require('./http.class.php');

    $msg = array(

            'formhash'=>'4f23e777',

            'message'=>'world',

            'pmsubmit'=>'true',

            'pmsubmit_btn'=>'发送',

            'refer'=>'http://localhost/foruser/HTTP/testhttp.php',

            'username'=>'http接收',

            'user'=>'Lover雪'

        );

    $http = new Http("http://localhost/foruser/HTTP/testhttp.php");

    $http->setHeader("cookie: user=Lover雪"); //使用COOKIE

    echo "<h1>POST请求</h1>";

    echo $http->post($msg);

    $http = new Http("http://localhost/foruser/HTTP/testhttp.php");

    $http->setHeader("cookie: user=Lover雪");

    echo "<h1>GET请求</h1>";

    echo $http->get();

    */

    ?>

    testhttp.php代码:

    <?php

    if(isset($_COOKIE['user']))

        echo "<h2>I Know You Are ".$_COOKIE['user']."</h2>";

    echo "<br/><br/>http:接收信息<br/>";

    if(isset($_POST))

        print_r($_POST);

    ?>

    实验结果图:

     

    ********************************************
    * 博客园: http://www.cnblogs.com/lihaiyan/
    * 邮箱:1063385677@qq.com
    * QQ: 1063385677
    * Copyright ©2014 Lover雪儿
    ********************************************
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。
  • 相关阅读:
    关于博客园创始人的心路历程,感触很深!
    Django中的ORM相关操作:F查询,Q查询,事物,ORM执行原生SQL
    在Django中运行脚本文件以及打印出SQL语句。
    Django中的跨表查询,多表查询。
    Django中的ORM介绍,字段以及字段的参数。
    9.2安全的 Web API 与 Web API 2.2 个人帐户
    9.1WebApi的身份验证和授权
    3.1创建项目
    2.4使用属性在 ASP.NET Web API 2 路由创建一个 REST API
    2.3属性在 ASP.NET Web API 2 路由
  • 原文地址:https://www.cnblogs.com/lihaiyan/p/4274191.html
Copyright © 2020-2023  润新知