PHP implements a method that mimics a socket request to return a page, Phpsocket
The example in this paper describes how the PHP implementation mimics the socket request return page. Share to everyone for your reference. The implementation method is as follows:
Copy CodeThe code is as follows: <?php
$url = "www.XXXX.com"; Do your own replacement
$parse = Parse_url ($url); Resolves the URL and returns the component.
$host = $parse [' Host '];
$path = $parse [' path '];
$port = 80;
$timeout = 80;
$fp = @fsockopen ($host, $port, $errno, $errstr, $timeout); Open the Socket link
if (! $fp) {
echo $errno. " --". $errstr; If error, return error code and error message
} else {
$out = "POST $path http/1.1\r\n"; The following is the HTTP request header information
$out. = "Host:". $host. " \ r \ n ";
$out. = "Accept: */*\r\n";
$out. = "connection:close\r\n";
$out. = "Cookie: $cookie \r\n\r\n";
@fwrite ($fp, $out); Write the request information to the link
$status = Stream_get_meta_data ($FP);
if (! $status [' timed_out ']) {
while (!feof ($fp)) {
if ($header = @fgets ($fp)) && ($header = = "\ r \ n" | | $header = = "\ n")) {
Break
}
}
$stop = false;
while (!feof ($FP) &&! $stop) {
$data = Fread ($fp, 8192); 8192 is the number of bytes that can be returned
$return. = $data;
}
}
Fclose ($FP);
Print_r ($return);
}
I hope this article is helpful to everyone's PHP programming.
PHP to a page 400 times the loop, each return time is variable, the result is often beyond the access time what should be done
1. Use JS Setinterval+ajax to request, PHP inside add conditions to determine whether the success;
2. Use the PHP recursive loop to do this, such as:
function test ($url)
{
echo $url. ' \t\n--------';
$r = @file_get_contents ($url);//I return the number of pages here, if there is no next page return 0, stop execution
Echo $r;
if ($r > 0)
{
$url = ' test.com/1.php?p= '. $r;
Test ($url);
}
Else
{
echo ' fail ';
}
}
$url = ' test.com/1.php?p=1 ';
$res = Test ($url);
?>
PHP uses sockets to get web content
function Getbysocket ($URL, $port =80) {
Get host from URL
Preg_match ('/\/\/.*\//su ', $URL, $host _array);
if (! $host _array[0]) {
$URL. = '/';
Preg_match ('/\/\/.*\//su ', $URL, $host _array);
}
$host =substr ($host _array[0],2,-1);
Connect
$fp = Stream_socket_client ("$host: $port", $errcode, $errstr, 1);//or Die ("get". $host. "Failed");
//
$header = "GET". $URL. "Http/1.1\r\n";
$header. = "Accept: */*\r\n";
$header. = "accept-language:zh-cn\r\n";
$header. = "http_connection:keep-alive\r\n";
$header. = "http_accept:text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/ Png,*/*;q=0.5\r\n ";
$header. = "http_accept_charset:gbk,*,utf-8\r\n";
$header. = "Accept-encoding:gzip, deflate\r\n";
$header. = "user-agent:mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;) \ r \ n ";
$header. = "Host:". $host. " \ r \ n ";
$header. = "connection:keep-alive\r\n";
$header. = "cookie:cnzz02=2; rtime=1; ltime=1148456424859; cnzz_eid=566 ... the rest of the full text >>
http://www.bkjia.com/PHPjc/906115.html www.bkjia.com true http://www.bkjia.com/PHPjc/906115.html techarticle PHP Implementation of the method of imitating the socket request to return the page, phpsocket The example of this article describes the PHP implementation to imitate the socket request to return the page method. Share to everyone for your reference. Concrete Implementation Method ...