【问题标题】:Obtaining standard port for arbitrary protocols in PHP在 PHP 中获取任意协议的标准端口
【发布时间】:2011-11-11 17:43:37
【问题描述】:

我正在寻找一个函数,该函数将接受表示 URL 的方案部分的字符串(例如,“http”、“https”、“ftp”等)并返回标准端口。可以这样使用:

echo get_port_from_protocol("http");  // 80

作为最后的手段,我想我可以写一些东西来解析/etc/services(假设我只需要在类 UNIX 操作系统下运行)。

但肯定有一些内置到 PHP 中,不是吗?

【问题讨论】:

    标签: php url protocols port


    【解决方案1】:
    int getservbyname ( string $service , string $protocol )
    

    http://php.net/manual/en/function.getservbyname.php

    所以基本上你可以做到

    echo getservbyname("http", "tcp");
    

    【讨论】:

      猜你喜欢
      • 2011-10-19
      • 2010-09-06
      • 2017-05-27
      相关资源
      最近更新 更多