首页 > 解决方案 > 当密钥存在时,phpredis rpop 返回 false

问题描述

我正在构建一个使用 redis 的游戏应用程序,这是我的 php 脚本。

<?php
//melon game pop

//connect to redis db
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);


//receive the data as json
//$data = json_decode(file_get_contents('php://input'), true);
$data = null;


//decided to just grab the string
$key = file_get_contents('php://input');

//Shotgun debugging in action
$my_key = trim($key);

/*
 was originally sending json on reading into variables from array decided to simplify until my issue is resolved
//parse my data in to separate variables  
if ($data != null) {
$key = $data['key'];
//$value = $data['value'];  

//$key = utf8_decode($key);        
}
else
{
print "It's not recognizing your data!";
}
*/




$my_echo = $redis->rpop($my_key);

 //it returns false if the key doesn't exist, but i've shown that it clearly exists so  why is it returning false???

if ($my_echo == false) {

print $my_key;
//print '5'.$key;

//print "5failure";

}
else
{
print $my_echo;
}
?>

我在上面的代码中所做的是一个游戏客户端将 $key 发送到 php,我想弹出存储在 $key 下的值,在这种情况下是“ubuntu5”。在我的调试块中,当我打印 $my_key 时,它以“ubuntu5”响应,所以我很难弄清楚为什么它不会打印存储在 $my_key 下的值。此外,当我从终端打开 redis-cli 并运行 rpop "ubuntu5" 时,它会以预期值响应。

关于这里可能发生什么的任何想法?{ "key": "\nubuntuleft22" } 我使用了 var_dump,并将其添加到脚本中:

$redis->lpush("my_control",$my_key);

当我弹出“my_control”时,我从一个客户端获得 { "key": "\nubuntuleft22" },而使用浏览器从另一个客户端获得 ubunturight22。当我从命令行手动弹出时,我得到以下信息:

redis-cli
127.0.0.1:6379> rpop my_control
"ubunturight22"
127.0.0.1:6379> rpop my_control
"{ \"key\": \"\\nubuntuleft23\" }"
127.0.0.1:6379> rpop "my_control"
"ubunturight23"
127.0.0.1:6379

标签: phpredisphpredis

解决方案


推荐阅读