HTTP Protocol Analysis Series (v)------Php+socket program send HTTP request ___ Programming

Source: Internet
Author: User
Tags php foreach

First, Php+socket request principle

second, simulate POST request

 

third, encapsulate their own HTTP class

 HTTP request Class Interface interface proto{//connection URL function conn ($url);
	Send a Get Query function get ();
	Send post query function post ();
Closes the connection function close ();
	Class Http implements proto{const crlf= "\ r \ n";
	protected $errno =-1;
	protected $errstr = ';
	protected $response = ';
	protected $url =null;
	protected $version = ' http/1.1 ';
	protected $FH =null;
	Protected $line =array ();
	Protected $header =array ();
	
	Protected $body =array ();
		Public function __construct ($url) {$this->conn ($url);
	$this->setheader (' Host: '. $this->url[' host ')); //This method is responsible for write request line protected function Setline ($method) {$this->line[0]= $method. ' '. $this->url[' path ']. '
	'. $this->version;
	}//This method is responsible for writing header information protected function SetHeader ($headerline) {$this->header[]= $headerline;
	}//This method is responsible for writing the principal information protected function Setbody ($body) {$this->body[]=http_build_query ($body);;
		}//Connection URL function conn ($url) {$this->url=parse_url ($url); Judge Port if (!isset ($this->url[' Port ')) {$this->url[' port']=80;
	$this->fh=fsockopen ($this->url[' host '), $this->url[' Port ', $this->errno, $this->errstr,3);
		//Constructs a GET Request data function get () {$this->setline (' get ');
		$this->request ();
	return $this->response;
		
		The data function post of the POST request ($body =array ()) {//constructs the body information $this->setline (' post ');
		Set Content-type $this->setheader (' content-type:application/x-www-form-urlencoded ');
		Set the body information, $this->setbody ($body) than the one where get is different;
		Compute content-length $this->setheader (' content-length: ' strlen ($this->body[0));
		$this->request ();
	return $this->response; ///Real Requests function request () {//the request line, header information, entity information in an array, easy to splice $req =array_merge ($this->line, $this->header,array ("
		), $this->body,array (")");
		$req =implode (Self::crlf, $req);
		
		Fwrite ($this->fh, $req);
		while (!feof ($this->fh)) {$this->response.=fread ($this->fh,1024);
	$this->close ();//close connection return $this->response; //Turn off connection function close () {FCLOSE ($this->FH); }
}

Four, the test function, realizes the batch post function

(i), the establishment of data tables

 

(ii), procedure realization

(1), establish liuyanben.php

  

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">  

(2). Realize Batch Post

 

Set_time_limit (0);
$url = ' http://localhost/socket/liuyanben.php ';

For ($i =1 $i <100; $i + +) {
	$str =str_shuffle (' abcdefghigklmnopqrstuvwxyz ');
	$title =substr ($str, 0,5);
	$content =substr ($str, 6,8);
	$http =new http ($url);
	$http->post (' title ' => $title, ' content ' => $content, ' Submit ' => ' message '));
	echo $title. '--------'. $con. ' <br/> ';
	Usleep ();
}
(iii), experimental results

 

View Page


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.