PHP implements the method of imitating socket request to return the page, phpsocket_PHP tutorial

WBOY
Release: 2016-07-13 10:15:09
Original
862 people have browsed it

PHP implements the method of imitating socket request to return the page, phpsocket

The example in this article describes how PHP implements a method of imitating a socket request to return a page. Share it with everyone for your reference. The specific implementation method is as follows:

Copy code The code is as follows:
$url = "www.XXXX.com"; //Make your own substitution
$parse = parse_url($url); //Parse the URL and return its components.
$host = $parse['host'];
$path = $parse['path'];
$port = 80;
$timeout = 80;
$fp = @fsockopen($host, $port, $errno, $errstr, $timeout); //Open socket link
if (!$fp){
Echo $errno."--".$errstr; //If error occurs, return error code and error message
} else {
$out = "POST $path HTTP/1.1rn"; //The following is the HTTP request header information
$out .= "Host: ".$host."rn";
$out .= "Accept: */*rn";
$out .= "Connection: Closern";
$out .= "Cookie: $cookiernrn";
 
@fwrite($fp, $out); //Write the request information into the link
$status = stream_get_meta_data($fp);
If(!$status['timed_out']) {
While (! Feof ($ fp)) {
If(($header = @fgets($fp)) && ($header == "rn" || $header == "n")) {
                                         break;                                                                                                                                                                                                                           
$stop = false;
                                                                                                                                                                                                                      while(!feof($fp) && !$stop) {                               $data = fread($fp,8192);                                $return .= $data;                                                                                                                                                                                                                                                                  fclose($fp);
Print_r($return);
}


I hope this article will be helpful to everyone’s PHP programming design.




php needs to loop through a page more than 400 times, and the return time is uncertain each time. The result is that the access time is often exceeded. What should I do?


1. Use js's setInterval+ajax to request, and add conditions in PHP to determine whether it is successful; 2. Use PHP recursive loops, such as:

function test($url)
{
echo $url.'\t\n--------';

$r = @file_get_contents($url);//What I returned here Is the number of pages. If there is no next page, return 0 and 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 socket to obtain 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......余下全文>>
 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/906115.htmlTechArticlePHP实现模仿socket请求返回页面的方法,phpsocket 本文实例讲述了PHP实现模仿socket请求返回页面的方法。分享给大家供大家参考。具体实现方法...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template