"使用未定义的常量成功 - 假设'成功'"错误解析json响应

Jul*_*eto 2 php json

即时通讯使用谷歌recaptcha和我的验证方式是通过ajax,一切正常,我得到了回应

{
success: "false/true"
}
Run Code Online (Sandbox Code Playgroud)

但我得到一个错误,说"使用未定义的不断成功 - 假设'成功'在第30行,任何想法,帮助,建议,建议?

这是我在控制器上的代码(用于传达google recaptcha API的代码)

$captcha=$request->input('g-recaptcha-response');
$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=mysitekey&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);
if($response.success == false) //this is the line 30
{
    return 'You are spammer ! Get the out';
}else{
    return 'Thanks for posting comment.';
}
Run Code Online (Sandbox Code Playgroud)

这是第30行"$ response.success == false"

Ale*_*rei 6

你错过了几个步骤.首先,您需要json_decode从API获得的响应

$responseData = json_decode($response);
Run Code Online (Sandbox Code Playgroud)

接下来,您需要正确地解决我认为将成为数组的"success"元素,但您将使用a来验证它var_dump().

var_dump($responseData);
Run Code Online (Sandbox Code Playgroud)

如果你得到一个数组,那么该if子句将如下所示:

if ($responseData["success"]==false)
Run Code Online (Sandbox Code Playgroud)

另一方面,如果你得到一个对象responseData,`if子句将如下所示

if ($responseData->success==false)
Run Code Online (Sandbox Code Playgroud)

该dot符号是Javascript中php的dot是concatentation运营商.