PHP中 fsockopen 函数怎么用?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了PHP中 fsockopen 函数怎么用?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1797字,纯文字阅读大概需要3分钟。
内容图文
![PHP中 fsockopen 函数怎么用?](/upload/InfoBanner/zyjiaocheng/126/af7479d136a64a86b34b2fa38745f6e0.jpg)
在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 函数怎么用?的详细内容。
内容总结
以上是互联网集市为您收集整理的PHP中 fsockopen 函数怎么用?全部内容,希望文章能够帮你解决PHP中 fsockopen 函数怎么用?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。