PHP中 fsockopen 函数怎么用?
程序员文章站
2022-03-26 13:11:57
...
在PHP中fsockopen函数的作用是打开一个网络连接或者一个Unix套接字连接,其语法为“fsockopen($hostname) ”,返回值为一个文件句柄,之后可以被其他文件类函数调用。
简单示例
<?php $fp = fsockopen("www.example.com", 80, $errno, $errstr, 30); if (!$fp) { echo "$errstr ($errno)<br />\n"; } else { $out = "GET / HTTP/1.1\r\n"; $out .= "Host: www.example.com\r\n"; $out .= "Connection: Close\r\n\r\n"; fwrite($fp, $out); while (!feof($fp)) { echo fgets($fp, 128); } fclose($fp); } ?>
使用UDP连接
<?php $fp = fsockopen("udp://127.0.0.1", 13, $errno, $errstr); if (!$fp) { echo "ERROR: $errno - $errstr<br />\n"; } else { fwrite($fp, "\n"); echo fread($fp, 26); fclose($fp); } ?>
使用示例
<?php $host = "something.example.com"; $port = 443; $path = "/the/url/path/file.php"; //or .dll, etc. for authnet, etc. //you will need to setup an array of fields to post with //then create the post string $formdata = array ( "x_field" => "somevalue"); //build the post string foreach($formdata AS $key => $val){ $poststring .= urlencode($key) . "=" . urlencode($val) . "&"; } // strip off trailing ampersand $poststring = substr($poststring, 0, -1); $fp = fsockopen("ssl://".$host, $port, $errno, $errstr, $timeout = 30); if(!$fp){ //error tell us echo "$errstr ($errno)\n"; }else{ //send the server request fputs($fp, "POST $path HTTP/1.1\r\n"); fputs($fp, "Host: $host\r\n"); fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n"); fputs($fp, "Content-length: ".strlen($poststring)."\r\n"); fputs($fp, "Connection: close\r\n\r\n"); fputs($fp, $poststring . "\r\n\r\n"); //loop through the response from the server while(!feof($fp)) { echo fgets($fp, 4096); } //close fp - we are done with it fclose($fp); }
推荐教程:《PHP》
以上就是PHP中 fsockopen 函数怎么用?的详细内容,更多请关注其它相关文章!
上一篇: MYSQL哪种查询更快一点,该怎么处理
下一篇: PHP学习之PHP的软件配置
推荐阅读
-
求php中的include()与自定义函数的优先级,该怎么解决
-
怎么正确运用PHP ini_set函数
-
一个高级java架构师用PHP语法回答了一个JS的问题被百度评为专业回答,请问百度专业回答怎么做到的?
-
如何用PHP中的mktime函数获取开始和结束时间
-
PHP dwt模板中怎么控制标题长度
-
PHP中trim()函数简单使用指南,trim使用指南_PHP教程
-
php中curl和file_get_content函数抓页面对比
-
各位大牛谁知道怎么用php 生成token 七牛云存储
-
php在做敏感词过滤时怎么解决用特殊符号分割、简繁体、半角全角,来绕开过滤的问题?
-
为什么php手册中好多函数参数后面都有半个中括号?这些括号有什么意义昵?