PHP中 fsockopen 函数怎么用?_后端开发

PHP中 fsockopen 函数怎么用?_后端开发,第1张

Go Wails 框架构建桌面应用示例_后端开发

这篇文章主要介绍了利用Go Wails 框架构建桌面应用示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

在PHP中fsockopen函数的作用是打开一个网络连接或者一个Unix套接字连接,其语法为“fsockopen($hostname) ”,返回值为一个文件句柄,之后可以被其他文件类函数调用。


PHP中 fsockopen 函数怎么用?_后端开发,第2张

简单示例

CentOS安装PHP7的正确方法_后端开发

CentOS上的PHP版本都十分古老,满足不了一些框架对PHP版本的要求。


于是,出现了许多第三方软件库,如EPEL、RPM Fusion、Remi等,这些库提供了新版的PHP。


让我们的系统能与时俱进。


<?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 函数怎么用?的详细内容,更多请关注ki4网其它相关文章!

Python 的 help 函数使用_后端开发

在本篇文章里小编给大家整理的是关于python的help函数的相关用法和知识点总结,需要的朋友们可以学习下。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/zaji/560845.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2020-06-24
下一篇 2020-06-24

发表评论

登录后才能评论

评论列表(0条)

保存