PHP socket sends HTTP request

Source: Internet
Author: User

<?php/** * Created by Phpstorm. * USER:MCH * DATE:7/8/18 * time:21:39@func:parse_urlhttp://www.tfjyzx.com/model-school.jsp?area=%e5%bc%80%e5%b0%81& amp;school=%e5%bc%80%e5%b0%81%e5%b8%82%e7%ac%ac%e4%ba%94%e4%b8%ad%e5%ad%a6#%e5%ad%a6%e6%a0%a1%e7%ae%80%e4%bb% 8Barray (5) {["Scheme"]=>string (4) "http" ["Host"]=>string "www.tfjyzx.com" ["Path"]=>string (17) "/ model-school.jsp "[" Query "]=>string (94)" Area=%e5%bc%80%e5%b0%81&school=%e5%bc%80%e5%b0%81%e5%b8%82%e7%ac %ac%e4%ba%94%e4%b8%ad%e5%ad%a6 "[" Fragment "]=>string"%e5%ad%a6%e6%a0%a1%e7%ae%80%e4%bb%8b "}*/interface    Proto {//Connection URL public Function conn ($url);    Send get query Public function get ();    Send post query Public function post (); Close connection Public function close ();}    Class Http implements Proto {Const CRLF = "\ r \ n";    Const BUFFSIZE = 1024;    protected $errno =-1;    protected $errstr = ";    protected $response = ";    protected $url = [];    protected $version = ' http/1.1 '; Protected$FH = null;    protected $line = [];    protected $header = [];    protected $body = [];        Public function __construct ($url) {$this->conn ($url);    $this->setheader (' Host: ' $this->url[' host ');        }//Post/student/login http/1.1 protected function Setline ($method) {$query = $this->url[' query ']; $this->line[0] = implode ("', [$method, $this->url[' path ']. ( Strlen ($query) >0? ‘?‘.    $query: "), $this->version]);    Public Function SetHeader ($headerline) {$this->header[] = $headerline;    Public Function Setbody ($body) {$this->body[] = http_build_query ($body);        }//Connection URL public Function conn ($url) {$this->url = Parse_url ($url);        if (!isset ($this->url[' Port ')) {$this->url[' port '] = 80;        } if (!isset ($this->url[' query ')) {$this->url[' query '] = '; } $this-&GT;FH = FsockopEn ($this->url[' host '), $this->url[' Port ', $this->errno, $this->errstr,        3/* Timeout 3s */);            if (! $this->fh) {echo "$this->errstr ($this->errno)";        return NULL;    } return $this->fh;        }//construct data for GET request public function get () {$this->setline (' get ');        $this->request ();    return $this->response; }//Construct data Public function post for post query ($body = [], $enctype = ' application/x-www-form-urlencoded ') {$this-&gt        ; Setline (' POST '); Content-type ' Multipart/form-data ' or ... $this->setheader (' Content-type: '. $enctype. ‘;        Charset=utf-8 ');        $this->setbody ($body);        $this->setheader (' content-length: '. strlen ($this->body[0]));        $this->request ();    return $this->response;    }//Close connection Public function close () {$this->fh && fclose ($this->fh); }//Real pleaseAsk for Private function request () {//Put the request line, header information, entity information in an array, easy to splice fwrite ($this->fh, implode (SELF::CRLF, arr Ay_merge ($this->line, $this->header, ["], $this->body, [        ‘‘] )));        Why is the loop slow for the 2nd time (assuming that the response length is <buffsize, i.e. 1 reads are done)?            while (!feof ($this->fh)) {$content = Fread ($this->fh, self::buffsize);            if (strlen ($content) <=0) {break;            } $this->response. = $content;        Echo $this->response; }}}//$url = "Http://www.tfjyzx.com/news/listTVProgram?area=%E8%AE%B8%E6%98%8C"; $url = "http://www.tfjyzx.com/ Student/login "; $http = new http ($url);//$resp = $http->get (); $http->setheader (' X-requested-with: XMLHttpRequest '); $http->setheader (' user-agent:mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) applewebkit/537.36 (khtml, like Gecko) chrome/67.0.3396.99 safari/537.36 ');//!important $.ajax () ContentType:' Application/json ' $http->setheader (' Accept:application/json, Text/javascript, */*; q=0.01 ');//' Username=  Mingzhanghui&pwd=123456&captcha=&count=1 ' $resp = $http->post ([' username ' = ' mingzhanghui ', ' pwd ') = ' 123456 ', ' captcha ', ' = ', ' + ', ' count ' = 1]); Var_dump ($http); $http->close (); Echo $resp;

  

PHP socket sends HTTP request

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.