从 PHP 连接到 OrientDB

发布于 2024-10-04 05:35:36 字数 2243 浏览 1 评论 0原文

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

但我需要有 PHP 原始套接字通信经验的人的帮助 - 我似乎无法克服将 PHP 连接到 OrientDB 的第一个障碍。

如果有套接字经验的人能看看这个:

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

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

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

希望有人可以帮助我开始吗?

谢谢!


11/20/2010

参考 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 下...

I would like to write an adapter for PHP for the binary API of OrientDB.

But I need a bit of help from someone who has experience with raw socket communications in PHP - I can't seem to even get past the first hurdle of connecting PHP to OrientDB.

I would appreciate it if someone experience with sockets would take a look at this:

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

If we could get past the first hurdle and actually send a packet (simplified examples at the bottom of that page - please scroll down to the end), I could certainly write the adapter.

And if I do, this would of course be released as open source.

Hoping someone can help me get started?

Thanks!


11/20/2010

Referencing PEAR's Net_Socket, I ended up with essentially the same code I attempted early on, using fsockopen() and the regular PHP stream-functions.

Still I got nowhere. The server does not react at all, and even with a 5 second timeout set, the script just goes into deep sleep, and doesn't come out until the general PHP script time limit is surpassed.

Here's the code:

<?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);

And here's the hex_dump() function I'm using to inspect the packet I'm submitting:

<?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;
  }
}

According to Luca Garulli, the author of OrientDB, the packet I'm submitting looks right. So something else is amiss...

Could this be a Windows issue? I'm using PHP 5.3 on Windows, under Apache...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我只土不豪 2024-10-11 05:35:36

事实上,我忘记了问题到底是什么 - 但我确实让它工作了。

如果其他人需要查看有效的实现(无论是 OrientDB 还是其他),请随时查看此处的公共存储库:

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

Actually, I forget what precisely the issue was - but I did get it working.

If anyone else needs to see a working implementation (whether for OrientDB or something else) feel free to take a look at the public repository here:

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

∝单色的世界 2024-10-11 05:35:36

这可能只是防火墙问题吗?添加 F/W 例外或暂时禁用 F/W。

是端口号。正确的?你能远程登录到那个端口吗?

> telnet 127.0.0.1 2424

Could this be simply a firewall issue? Add a F/W exception or disable F/W temporarily.

Is the port nr. correct? Can you telnet to that port?

> telnet 127.0.0.1 2424
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文