dongzha5934
2012-03-09 21:38 阅读 50
已采纳

如何使用php连接到redis但不使用客户端库

I'd like know what is the way to connect to redis using php from scratch (without use a client like predis)?

Thanks.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    dpruwm6206 dpruwm6206 2012-03-09 22:22

    You can connect by using fsockopen and communicate by sending raw commands and reading server response:

    $c = fsockopen('127.0.0.1', 6379, $errCode, $errStr);
    
    $rawCommand = "*2
    \$4
    Echo
    \$12
    hello world!
    ";
    
    fwrite($c, $rawCommand);
    
    $rawResponse = fgets($c);
    echo $rawResponse; // $12
    
    $rawResponse = fgets($c);
    echo $rawResponse; // hello world!
    

    To use it this way, you should get familiar with Redis protocol: http://redis.io/topics/protocol

    点赞 评论 复制链接分享
  • dsfsda121545 dsfsda121545 2012-03-09 22:38

    Using a raw TCP connection is one way.

    You could easily reference the Predis code to see how they connect.

    https://github.com/nrk/predis/blob/v0.7/lib/Predis/Network/StreamConnection.php::tcpStreamInitializer()

    And that is basically a wrapper for http://php.net/stream_socket_client : So I would reference that page for the most verbose info.

    If I am not mistaken, it would be the same as making a TELNET connection in the command line and interfacing with Redis in that manner.

    Hope that helps.

    点赞 评论 复制链接分享

相关推荐