php Zookeeper使用踩坑

用的是Zookeeper扩展,Php版本为7.2.17,下载地址:

https://pecl.php.net/package/zookeeper

 

用的是0.6.4版本

 

创建节点官方给的示例如下:

<?php
$zookeeper = new Zookeeper(‘locahost:2181‘);
$aclArray = array(
  array(
    ‘perms‘  => Zookeeper::PERM_ALL,
    ‘scheme‘ => ‘world‘,
    ‘id‘     => ‘anyone‘,
  )
);
$path = ‘/path/to/newnode‘;
$realPath = $zookeeper->create($path, null, $aclArray);
if ($realPath)
  echo $realPath;
else
  echo ‘ERR‘;
?>

  

一直报无效的acl,查看源代码:

static void php_parse_acl_list(zval *z_acl, struct ACL_vector *aclv)
{
    int size = 0;
    int i = 0;
#ifdef ZEND_ENGINE_3
    ulong index = 0;
    zend_string *key;
    zval *entry = NULL;
    zval *perms = NULL;
    zval *scheme = NULL;
    zval *id = NULL;
#else
    zval **entry;
    zval **perms, **scheme, **id;
#endif

    if (!z_acl || (size = zend_hash_num_elements(Z_ARRVAL_P(z_acl))) == 0) {
        return;
    }

    aclv->data = (struct ACL *)calloc(size, sizeof(struct ACL));

#ifdef ZEND_ENGINE_3
    ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(z_acl), index, key, entry) {
        if( Z_TYPE_P(entry) != IS_ARRAY ) {
            continue;
        }

        perms = zend_hash_str_find(Z_ARRVAL_P(entry), ZEND_STRL("perms"));
        scheme = zend_hash_str_find(Z_ARRVAL_P(entry), ZEND_STRL("scheme"));
        id = zend_hash_str_find(Z_ARRVAL_P(entry), ZEND_STRL("id"));
        if (perms == NULL || scheme == NULL || id == NULL) {
            continue;
        }

        convert_to_long_ex(perms);
        convert_to_string_ex(scheme);
        convert_to_string_ex(id);

        aclv->data[i].perms = (int32_t)Z_LVAL_P(perms);
        aclv->data[i].id.id = strdup(Z_STRVAL_P(id));
        aclv->data[i].id.scheme = strdup(Z_STRVAL_P(scheme));

        i++;
    } ZEND_HASH_FOREACH_END();

#else
    for (zend_hash_internal_pointer_reset(Z_ARRVAL_P(z_acl));
         zend_hash_get_current_data(Z_ARRVAL_P(z_acl), (void**)&entry) == SUCCESS;
         zend_hash_move_forward(Z_ARRVAL_P(z_acl))) {

        if (Z_TYPE_PP(entry) != IS_ARRAY) {
            continue;
        }

        perms = scheme = id = NULL;
        zend_hash_find(Z_ARRVAL_PP(entry), ZEND_STRS("perms"), (void**)&perms);
        zend_hash_find(Z_ARRVAL_PP(entry), ZEND_STRS("scheme"), (void**)&scheme);
        zend_hash_find(Z_ARRVAL_PP(entry), ZEND_STRS("id"), (void**)&id);
        if (perms == NULL || scheme == NULL || id == NULL) {
            continue;
        }

        convert_to_long_ex(perms);
        convert_to_string_ex(scheme);
        convert_to_string_ex(id);

        aclv->data[i].perms = (int32_t)Z_LVAL_PP(perms);
        aclv->data[i].id.id = strdup(Z_STRVAL_PP(id));
        aclv->data[i].id.scheme = strdup(Z_STRVAL_PP(scheme));

        i++;
    }
#endif

    aclv->count = i;
}

 

源码里把acl参数当作数组的数组了,所以调整下就可以了:

<?php
$zookeeper = new Zookeeper(‘locahost:2181‘);
$aclArray = [];
$aclArray[] = array(
  array(
    ‘perms‘  => Zookeeper::PERM_ALL,
    ‘scheme‘ => ‘world‘,
    ‘id‘     => ‘anyone‘,
  )
);
$path = ‘/path/to/newnode‘;
$realPath = $zookeeper->create($path, null, $aclArray);
if ($realPath)
  echo $realPath;
else
  echo ‘ERR‘;
?>

  

php Zookeeper使用踩坑

上一篇:ASP.Net中的async+await异步编程


下一篇:JS加载xml文件内容