我正在开发一个基于教程的 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);
谢谢