This article describes how to implement httpRequest in php. It involves php http operations and has some reference value. For more information, see
This article describes how to implement httpRequest in php. It involves php http operations and has some reference value. For more information, see
This example describes how to implement httpRequest in php. Share it with you for your reference. The details are as follows:
I tried many methods to capture data from the website of the school library and then return it to the browser. First, I tried http_request (), but this school's pecl_http support, and later I tried the class HttpRequest, which is widely circulated on the Internet. It may be that I won't use it, but it also failed. Then we can see the function httpRequest ($ url, $ post = '', $ method = 'get', $ limit = 0, $ returnHeader = FALSE, $ cookie = '', $ bysocket = FALSE, $ ip = '', $ timeout = 15, $ block = TRUE). You can use it to share it. The function code is as follows:
The Code is as follows:
<? Php
/**
* Respose A Http Request
*
* @ Param string $ url
* @ Param array $ post
* @ Param string $ method
* @ Param bool $ returnHeader
* @ Param string $ cookie
* @ Param bool $ bysocket
* @ Param string $ ip
* @ Param integer $ timeout
* @ Param bool $ block
* @ Return string Response
*/
Function httpRequest ($ url, $ post = '', $ method = 'get', $ limit = 0, $ returnHeader = FALSE, $ cookie ='', $ bysocket = FALSE, $ ip = '', $ timeout = 15, $ block = TRUE ){
$ Return = '';
$ Matches = parse_url ($ url );
! Isset ($ matches ['host']) & $ matches ['host'] = '';
! Isset ($ matches ['path']) & $ matches ['path'] = '';
! Isset ($ matches ['query']) & $ matches ['query'] = '';
! Isset ($ matches ['Port']) & $ matches ['Port'] = '';
$ Host = $ matches ['host'];
$ Path = $ matches ['path']? $ Matches ['path']. ($ matches ['query']? '? '. $ Matches ['query']: ''): 'http: // www.jb51.net /';
$ Port =! Empty ($ matches ['Port'])? $ Matches ['Port']: 80;
If (strtolower ($ method) = 'post '){
$ Post = (is_array ($ post) and! Empty ($ post ))? Http_build_query ($ post): $ post;
$ Out = "POST $ path HTTP/1.0 \ r \ n ";
$ Out. = "Accept: */* \ r \ n ";
// $ Out. = "Referer: $ boardurl \ r \ n ";
$ Out. = "Accept-Language: zh-cn \ r \ n ";
$ Out. = "Content-Type: application/x-www-form-urlencoded \ r \ n ";
$ Out. = "User-Agent: $ _ SERVER [HTTP_USER_AGENT] \ r \ n ";
$ Out. = "Host: $ host \ r \ n ";
$ Out. = 'content-Length: '. strlen ($ post). "\ r \ n ";
$ Out. = "Connection: Close \ r \ n ";
$ Out. = "Cache-Control: no-cache \ r \ n ";
$ Out. = "Cookie: $ cookie \ r \ n ";
$ Out. = $ post;
} Else {
$ Out = "GET $ path HTTP/1.0 \ r \ n ";
$ Out. = "Accept: */* \ r \ n ";
// $ Out. = "Referer: $ boardurl \ r \ n ";
$ Out. = "Accept-Language: zh-cn \ r \ n ";
$ Out. = "User-Agent: $ _ SERVER [HTTP_USER_AGENT] \ r \ n ";
$ Out. = "Host: $ host \ r \ n ";
$ Out. = "Connection: Close \ r \ n ";
$ Out. = "Cookie: $ cookie \ r \ n ";
}
$ Fp = fsockopen ($ ip? $ Ip: $ host), $ port, $ errno, $ errstr, $ timeout );
If (! $ Fp) return ''; else {
$ Header = $ content = '';
Stream_set_blocking ($ fp, $ block );
Stream_set_timeout ($ fp, $ timeout );
Fwrite ($ fp, $ out );
$ Status = stream_get_meta_data ($ fp );
If (! $ Status ['timed _ out']) {// No timeout
While (! Feof ($ fp )){
$ Header. = $ h = fgets ($ fp );
If ($ h & ($ h = "\ r \ n" | $ h = "\ n") break;
}
$ Stop = false;
While (! Feof ($ fp )&&! $ Stop ){
$ Data = fread ($ fp, ($ limit = 0 ||$ limit> 8192? 8192: $ limit ));
$ Content. = $ data;
If ($ limit ){
$ Limit-= strlen ($ data );
$ Stop = $ limit <= 0;
}
}
}
Fclose ($ fp );
Return $ returnHeader? Array ($ header, $ content): $ content;
}
}
?>
The call is also very simple. Simple Example:
The Code is as follows:
Echo httpRequest ('HTTP: // www.baidu.com ');
I hope this article will help you with php programming.
,