Home  >  Article  >  Backend Development  >  PHP uses fsockopen function to send post, get request method to obtain web page content, fsockopen web page content_PHP tutorial

PHP uses fsockopen function to send post, get request method to obtain web page content, fsockopen web page content_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:13:591051browse

php uses fsockopen function to send post, get request method to obtain web content, fsockopen web content

The example in this article describes how PHP uses the fsockopen function to send post and get request to obtain the content of the web page. Share it with everyone for your reference.

The specific implementation code is as follows:

Copy code The code is as follows:
$post =1;
$url = parse_url($url);
$host ='http://www.bkjia.com';
$path ='/';
$query ='?action=phpfensi.com';
$port =80;

if($post) {
$out = "post $path http/1.0 ";
$out .= "accept: */* ";
//$out .= "referer: $boardurl ";
$out .= "accept-language: zh-cn ";
$out .= "content-type: application/x-www-form-urlencoded ";
$out .= "user-agent: $_server[http_user_agent] ";
$out .= "host: $host ";
$out .= 'content-length: '.strlen($post)." ";
$out .= "connection: close ";
$out .= "cache-control: no-cache ";
$out .= "cookie: $cookie ";
$out .= $post;
} else {
$out = "get $path http/1.0 ";
$out .= "accept: */* ";
//$out .= "referer: $boardurl ";
$out .= "accept-language: zh-cn ";
$out .= "user-agent: $_server[http_user_agent] ";
$out .= "host: $host ";
$out .= "connection: close ";
$out .= "cookie: $cookie ";
}
$fp = @fsockopen(($ip ? $ip : $host), $port, $errno, $errstr, $timeout);
if(!$fp)
{
return '';//note $errstr : $errno
} else {
Return 'successful access';
}

fsockopen syntax:
Copy code The code is as follows:
resource fsockopen(string $hostname [,int $port = -1 [, int &$errno [ ,string &$errstr [, float $timeout = ini_get("default_socket_timeout") ]]]] )

Starts a socket connection to the specified host resource. PHP supports the Internet domain targets and Unix in the supported socket transport list. The supported transport list can also be retrieved using stream_get_transports().

This socket will be enabled by default in blocking mode. You can switch to non-blocking mode using stream_set_blocking(). If you don’t understand the above example, let’s take a look at a simple one. The code is as follows:

Copy code The code is as follows:
$fp = fsockopen("www.jb51.net", 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno) ";
} else {
$out = "get / http/1.1 ";
$out .= "host: www.jb51.net";
$out .= "connection: close ";
fwrite($fp, $out);
while (!feof($fp)) {
echo fgets($fp, 128);
}
fclose($fp);
}

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/912282.htmlTechArticlephp uses fsockopen function to send post, get request method to obtain web page content, fsockopen web page content This article describes the use of php The fsockopen function sends a post and a get request to obtain the content of the web page...
Statement:
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