Php implementation of httpRequest

Source: Internet
Author: User
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.

,

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.