当前位置:网站首页>Application of fsockopen function
Application of fsockopen function
2022-06-29 02:26:00 【hello php】
demo as follows :
//fsockopen() Used to open a socket network connections , Similar to python The following code :
"""
# initialization socket
client = socket.socket()
# Link server
client.connect(('127.0.0.1', 8000))
# Send information to the server
client.send(b'this is client')
# Accept server messages , At most... At one time 1024 Bytes
recive_msg = client.recv(1024)
"""
$fp = fsockopen("www.baidu.com", 80, $errno, $errstr, 30);
if (!$fp) {
echo '<pre>';
echo '*****************************************';
echo "$errstr ($errno)<br />\n";
echo '*****************************************';
echo '</pre>';
} else {
// Build a response to the browser http Header information
$out = "GET / HTTP/1.1\r\n";
$out .= "Host: baidu.com\r\n";
$out .= "name:longqiqi\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
echo '<pre>';
echo '------------------------------------------------';
while (!feof($fp)) {
echo fgets($fp, 128);
}
echo '------------------------------------------------';
echo '</pre>';
fclose($fp);
}else In structure $out Is to build the response to the browser http Header information format , There can be no shortage of .
Splicing HTTP After header information , hold fsockopen The obtained web page content is given to the browser accordingly
demo2
$url = "http://localhost/test/test.php"; #url Address must be http://xxxxx
$port = 80;
$t = 30;
$data = array(
'foo' => 'bar',
'baz' => 'boom',
'site' => 'www.manongjc.com',
'name' => 'nowa magic');
/**fsockopen Grab page
* @parem $url Web address host The host address
* @parem $port URL port Default 80
* @parem $t Script request time Default 30s
* @parem $method Request mode get/post
* @parem $data If the data transmitted separately is post The way
* @return Return the requested data
* */
function sock_data($url, $port = 80, $t = 30, $method = 'get', $data = null)
{
$info = parse_url($url);
$fp = fsockopen($info["host"], $port, $errno, $errstr, $t);
// Judge whether there is data
if (isset($data) && !empty($data)) {
$query = http_build_query($data); // Array rotation url String form
} else {
$query = null;
}
// If the user's $url "http://www.manongjc.com/"; The lack of Last backslash
if (!isset($info['path']) || empty($info['path'])) {
$info['path'] = "/index.html";
}
// Judge Request mode
if ($method == 'post') {
$head = "POST " . $info['path'] . " HTTP/1.0" . PHP_EOL;
} else {
$head = "GET " . $info['path'] . "?" . $query . " HTTP/1.0" . PHP_EOL;
}
$head .= "Host: " . $info['host'] . PHP_EOL; // Request host address
$head .= "Referer: http://" . $info['host'] . $info['path'] . PHP_EOL;
if (isset($data) && !empty($data) && ($method == 'post')) {
$head .= "Content-type: application/x-www-form-urlencoded" . PHP_EOL;
$head .= "Content-Length: " . strlen(trim($query)) . PHP_EOL;
$head .= PHP_EOL;
$head .= trim($query);
} else {
$head .= PHP_EOL;
}
$write = fputs($fp, $head); // write file ( Safe for binary ). fputs() The function is fwrite() Alias for function
while (!feof($fp)) {
$line = fread($fp, 4096);
echo $line;
}
}
// Function call
sock_data($url, $port, $t, 'post', $data);
边栏推荐
- Google Borg论文
- String method exercise
- [redis] sortedset type
- SAP ui5 beginner tutorial 22 - development and use of filter
- What is the Valentine's Day gift given by the operator to the product?
- 【学习笔记】子集和问题
- 瀑布型项目管理最常用的10个小工具,可以自由搭建使用
- Differences between web testing and app testing
- Temperature conversion II
- Koa quick start
猜你喜欢
![[redis] key hierarchy](/img/ab/a5d3bb61b4571966d0f47037af4f41.png)
[redis] key hierarchy

Talk about the copyonwritearraylist of JUC

Ctfhub web password weak password

【Redis】Hash类型

The left toolbar of hbuilder is missing

What is the Valentine's Day gift given by the operator to the product?

安装kibana

Has Moore's law come to an end?

矩阵特征值和特征向量求解——特征值分解(EVD)
![[high concurrency, high performance and high availability of massive data MySQL practice-10] - Implementation of mvcc in InnoDB](/img/dc/a30ccd9943e668aef8c874980a4975.jpg)
[high concurrency, high performance and high availability of massive data MySQL practice-10] - Implementation of mvcc in InnoDB
随机推荐
短视频平台常见SQL面试题,你学会了吗?
Qt基础教程:QStringList
Smart world 2030
Trigonometric function calculation
大智慧手机股票开户哪个券商更安全更方便?
计算矩形面积
Qt基础教程:数据类型与容器
SystemVerilog structure (I)
Talk about the copyonwritearraylist of JUC
Learning Tai Chi Maker - mqtt Chapter II (IX) test of this chapter
Ctfhub web password weak password
What is Mipi
【Redis】Hash类型
Tuples of combined data types
干货丨微服务架构是什么?有哪些优点和不足?
What is the dry goods microservice architecture? What are the advantages and disadvantages?
东方财富股票开户是会有什么风险吗?东方财富开户安全吗
正则表达式(?:pattern)
温度转换 II
[redis] hash type