代码之家  ›  专栏  ›  技术社区  ›  mindplay.dk

从php连接到orientdb

  •  4
  • mindplay.dk  · 技术社区  · 14 年前

    我想为OrientDB的二进制API编写一个PHP适配器。

    但我需要有经验的人来帮助我,他们在PHP中使用原始套接字通信,我甚至无法跨越将PHP连接到orientdb的第一个障碍。

    如果有人有插座方面的经验,我会很感激的:

    http://code.google.com/p/orient/issues/detail?id=126

    如果我们能够越过第一个障碍并实际发送数据包(页面底部的简化示例-请向下滚动到末尾),我当然可以编写适配器。

    如果我这样做,这当然会作为开放源码发布。

    希望有人能帮我开始?

    谢谢!


    2010年11月20日

    参考Pear的net_socket,我使用fsockopen()和常规的php流函数,最终得到了我早期尝试的基本相同的代码。

    但我还是一无所获。服务器根本没有反应,即使设置了5秒的超时,脚本也只是进入深度睡眠状态,直到超过了常规的PHP脚本时间限制才出来。

    代码如下:

    <?php
    
    header('Content-type: text/plain');
    
    error_reporting(E_ALL | E_NOTICE | E_WARNING);
    
    $txid = 123;
    $db = 'demo';
    $username = 'writer';
    $password = 'writer';
    
    $packet = "\x05". # 1 byte
      pack('i',$txid). # 4 bytes
      pack('i',strlen($db)).$db. # string
      pack('i',strlen($username)).$username. # string
      pack('i',strlen($password)).$password; # string
    
    hex_dump($packet);
    
    $addr = '127.0.0.1';
    $port = 2424;
    $timeout = 5;
    $errstr = '';
    $errno = 0;
    
    $socket = fsockopen($addr, $port, $errno, $errstr, $timeout);
    
    stream_set_blocking($socket, 1);
    
    socket_set_timeout($socket, $timeout);
    
    var_dump($socket);
    
    fwrite($socket, $packet);
    
    $response = '';
    while (!feof($socket))
      $response .= fread($socket, 1024);
    
    hex_dump($response);
    
    fclose($socket);
    

    下面是我用来检查我提交的包的hex_dump()函数:

    <?php
    
    function hex_dump($data, $newline="\n")
    {
      static $from = '';
      static $to = '';
    
      static $width = 16; # number of bytes per line
    
      static $pad = '.'; # padding for non-visible characters
    
      if ($from==='')
      {
        for ($i=0; $i<=0xFF; $i++)
        {
          $from .= chr($i);
          $to .= ($i >= 0x20 && $i <= 0x7E) ? chr($i) : $pad;
        }
      }
    
      $hex = str_split(bin2hex($data), $width*2);
      $chars = str_split(strtr($data, $from, $to), $width);
    
      $offset = 0;
      foreach ($hex as $i => $line)
      {
        echo sprintf('%6X',$offset).' : '.implode(' ', str_split($line,2)) . ' [' . $chars[$i] . ']' . $newline;
        $offset += $width;
      }
    }
    

    据OrientDB的作者Luca Garulli说,我提交的包看起来不错。所以还有什么不对劲的…

    这可能是Windows问题吗?我在windows上使用php 5.3,在apache下…

    2 回复  |  直到 14 年前
        1
  •  1
  •   mindplay.dk    14 年前

    事实上,我忘记了问题的确切内容——但我确实做到了。

    如果其他人需要看到一个正在运行的实现(无论是针对OrientDB还是其他什么),请随意查看下面的公共存储库:

    https://github.com/mindplay-dk/OrientDB-PHP

        2
  •  0
  •   rustyx    14 年前

    这可能只是防火墙问题吗?添加固件例外或临时禁用固件。

    端口号正确吗?你能打电话到那个港口吗?

    > telnet 127.0.0.1 2424