PHP emulates the code for implementing HTTP requests in XmlHttpRequest in asp

  • 2020-03-31 21:39:36
  • OfStack

The name of the class: HttpRequest ($url = "", $method =" GET ", $useSocket = 0)
//$url is the requested address; The default request method is GET; $useSocket defaults to 0, using the fsockopen method, and to 1 using the socket_create method

Methods:
Open ($IP ="",$port=-1) // open the connection with the server, and these two parameters are not set by default (when a colleague is using Linux, what he requests is not the IP resolved by hostname, so he adds these two parameters to connect to the real server IP)
SetTimeout ($timeout=0) // setTimeout($timeout=0) // setTimeout($timeout=0
SetRequestHeader ($key,$value="") // sets the request header, which must be set before the send method is called
RemoveRequestHeader ($key,$value="") // removes the request header for the specified key value and must be called before the send method is called
Send ($data="") // send $data to the server
GetResponseBody () // gets the text returned by the server
GetAllResponseHeaders () // gets all header information for the server response
GetResponseHeader ($key) // gets some header information of the Server response, such as Server,Set_Cookie, etc

Properties:
$url // the url to be requested
$method // request method (POST/GET)
$port // requested port
$hostname // requested hostname
The file part of the $uri //url
$protocol // request protocol (HTTP)(including the above 5 attributes of this property are automatically analyzed by the program through url)
$excption // exception information
$_headers=array() // request header array("key"=> Value ")
$_senddata // data sent to the server
$status // returns the status code
$statusText // status information
$HttpProtocolVersion // the HTTP protocol version of the server

Note:
The Host header is set automatically by the program, and the content-length and content-type are set automatically when requested using the POST method.
Pages that support gzip compression
(link: http://xiazai.jb51.net/201103/yuanma/php_XmlHttpRequest.rar)

Related articles: