首页 > 解决方案 > 在数字前使用 {%2B) 而不是 (+) 有什么区别?

问题描述

我正在开发一个基于教程的 api,它从用户那里获取令牌以识别该用户是否是管理员。

所以这只是一个问题,这真的很重要吗

+1xxx-xxxx-xxx

或者

%2B1xxx-xxxx-xxx

当我测试我的 api 时,如果我在 isServerToken 中输入错误的数据,它会返回 null,如果我使用正确的数据,它会返回正确的值。但是如果我使用它+而不是[%2B]它会给我这个错误:

<br />
<b>Notice</b>:  Undefined variable: token in <b>/h**e/m***/pu**tml/***/db_functions.php</b> on line <b>389</b><br />
null

我将在下面评论 389 行

无论我对 isServerToken 使用正确还是错误的输入。它会说顶级错误。

我问这个是因为我从我的 android 收到了关于

java.langillegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $

所以我正在考虑导致此错误发生的所有可能性。这是PHP API. 在教程中,讲师使用了 get_result,但我已将其更改为 bind_result,因为它不适用于我的在线主机。

这是代码:

// Instructer Code
public function getToken($phone,$isServerToken)
{
    $stmt = $this->conn->prepare("SELECT * FROM `token` WHERE phone=? AND isServerToken=?") or die ($this->conn->error);
    $stmt->bind_param("ss",$phone,$isServerToken);
    $result = $stmt->execute();
    $token = $stmt->get_result()->fetch_assoc();
    $stmt->close();
    return $token;
}

// What i've changed to Bind
public function getToken($phone,$isServerToken)
{
    $stmt = $this->conn->prepare("SELECT phone, token, isServerToken FROM `token` WHERE phone=? AND isServerToken=?") or die ($this->conn->error);
    $stmt->bind_param("ss",$phone,$isServerToken);
    $result = $stmt->execute();
    $stmt->bind_result($arr['phone'], $arr['token'], $arr['isServerToken']);
    while ($stmt->fetch())
    {
        $token[] = $arr;
    }
    $stmt->close();
    return $token; => This is where `Undefined variable: token in` happen.
}

从这部分调用应用程序:

//Only instructor code, i didn't changed anything this part
if(isset($_POST['phone']) && isset($_POST['isServerToken']))
{
$userPhone = $_POST['phone'];
$isServerToken = $_POST['isServerToken'];

$token = $db->getToken($userPhone,$isServerToken);

echo json_encode($token);


}
else{
$response = "Required parameter (phone , isServerToken) is missing!";
echo json_encode($response);
}

我想确保当使用带有 +xxxx-xxxx-xxx 编号的寄存器时,这是否会使我的 api 说顶部错误,因为正如我所说,它与 %2Bxxxx-xxxx-xxx 一起工作正常。

数字也保存+在数据库中。

当我根据建议对这个基础提出意见时,事情变得相反现在+工作并且2%B将是空的。$token = $db->getToken(urlencode($userPhone),$isServerToken);

谢谢

标签: phpandroidmysqlapiretrofit2

解决方案


在您的第二个函数声明中,如果没有循环构造(因为您在循环内在线声明),则永远不会创建getToken变量。$tokenwhile$token

因此,可能的结果是该变量$token在返回的那一刻不存在$token(这就是您所经历的)。换句话说,在您的情况下没有任何结果被循环。

另外:加号在 JSON 中不是很好的匹配: https ://stackoverflow.com/a/1374456/5682311

urlencode在将其解析为之前在电话号码(或任何值)上使用php的函数json_encode,听起来像是解决方案(它将+符号转换为%2B

编辑:

public function getToken($phone,$isServerToken)
{
    $stmt = $this->conn->prepare("SELECT phone, token, isServerToken FROM `token` WHERE phone=? AND isServerToken=?") or die ($this->conn->error);
    $stmt->bind_param("ss",$phone,$isServerToken);
    $result = $stmt->execute();
    $stmt->bind_result($arr['phone'], $arr['token'], $arr['isServerToken']);

    $token = array(); // <- added this line, to be sure variable $token always exists
    while ($stmt->fetch())
    {
        $token[] = array_map('urlencode', $arr ); // <- urlencode all values within $arr
    }
    $stmt->close();
    return $token; 
}

推荐阅读