2018年6月

PHP libcurl 封装异步并发 HTTP 客户端

PHP 标准库内置 curl 扩展,不过实现不完整,如 multi_socket_action 接口。

无意中发现 pecl http 库同样基于 libcurl 封装,支持更多的libcurl特性,更新也比较快,底层通过 libevent(epoll) 实现 multi_socket_action 接口。

不过 pecl http 版本1和版本2 api完全不兼容,使用过程中稳定性及性能并不如PHP内置的curl,好像还有内存泄露,以下为示例代码,基于 pecl_http 2.20

<?php
function push($client, $url) {
  $req = new http\Client\Request("GET", $url, ["User-Agent"=>"My Client/0.1"]);
  $req->setOptions(array('connecttimeout'=>1, 'timeout'=>1));
  $client->enqueue($req, function($response) use ($client, $req, $url) {
     printf("%s returned '%s' (%d)\n", $response->getTransferInfo("effective_url"), $response->getInfo(), $response->getResponseCode());
     echo $client->count().PHP_EOL;
     global $urls;
     if ($urls) {
        while ($client->count() < 20) {
           $url = array_shift($urls);
           push($client, $url);
        }
        return true; // dequeue
     }
  });
}

$client = new http\Client;
$client->enablePipelining(true);
$client->enableEvents(true);

for ($i = 0; $i < 10000; ++$i) {
  $urls[] = "http://192.168.1.3/";
}
for ($i = 0; $i < 20; ++$i) {
  $url = array_shift($urls);
  push($client, $url);
}
/*
try{
  var_dump($client->send());
}
catch(http\Exception\RuntimeException  $e)
{
  echo 'Message: ' .$e->getMessage().PHP_EOL;
}
*/

while ($client->once()) {
  $client->wait();
}

- 阅读剩余部分 -

Bootstrap V4 实现自动下拉菜单

1. 前言

现在网 原创,换成 Bootstrap V4 后,好多在 Bootstrap V3 能用 JavaScript 实现的效果都变了,暂时未在官方文档找到 JavaScript 插件 的用法,只能自己乱搞一下了

2. 使用 JavaScript 实现

2-1. 需引入 Bootstrap V4bootstrap.jsbootstrap.min.js(压缩版) 和 jQuery.js

JavaScript 代码如下:

$('li.nav-item.dropdown').mouseover(function() {
    /*当鼠标经过时下拉(打开)*/
    $(this).addClass('show');
    $(this).children([2]).addClass('show');
}).mouseout(function() {
    /*当鼠标离开时时合起(关闭)*/
    $(this).removeClass('show');
    $(this).children([2]).removeClass('show');
});

效果图:

QQ截图20180610164616.png

3. 实例

粘贴就可以查看效果了
HTML 在线运行:http://www.runoob.com/runcode
<!doctype html>
<html lang="zh-CN">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
        <meta name="description" content="现在工具网">
        <meta name="author" content="陈文州">

        <title>现在工具网</title>
        <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet">
    </head>
    <body>

        <nav class="navbar navbar-expand-md navbar-dark fixed-top bg-dark">
            <a class="navbar-brand" href="#">现在工具网</a>
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarsDefault" aria-controls="navbarsExampleDefault" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>

            <div class="collapse navbar-collapse" id="navbarsDefault">
                <ul class="navbar-nav mr-auto">
                    <li class="nav-item active">
                        <a class="nav-link" href="./">首页</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/login">登陆账号</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/register">注册账号</a>
                    </li>
                    <li class="nav-item dropdown">
                        <a class="nav-link dropdown-toggle" href="" id="more_website" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">旗下网站</a>
                        <div class="dropdown-menu" aria-labelledby="more_website">
                            <a class="dropdown-item" href="https://nowtime.cc" target="_blank">现在网|个人博客</a>
                            <a class="dropdown-item" href="https://nowtool.cn" target="_blank">现在工具网</a>
                        </div>
                    </li>
                    <li class="nav-item dropdown">
                        <a class="nav-link dropdown-toggle" href="" id="more_website" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">旗下网站</a>
                        <div class="dropdown-menu" aria-labelledby="more_website">
                            <a class="dropdown-item" href="https://nowtime.cc" target="_blank">现在网|个人博客</a>
                            <a class="dropdown-item" href="https://nowtool.cn" target="_blank">现在工具网</a>
                        </div>
                    </li>
                </ul>
                <form class="form-inline my-2 my-lg-0">
                    <input class="form-control mr-sm-2" type="text" placeholder="Search" aria-label="Search">
                    <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
                </form>
            </div>
        </nav>


        <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
        <script src="https://cdn.bootcss.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
        <script>
            $('li.nav-item.dropdown').mouseover(function() {
                /*当鼠标经过时下拉(打开)*/
                $(this).addClass('show');
                $(this).children([2]).addClass('show');
            }).mouseout(function() {
                /*当鼠标离开时时合起(关闭)*/
                $(this).removeClass('show');
                $(this).children([2]).removeClass('show');
            });
        </script>
    </body>
</html>