您可能需要一个分号来执行代码。此外,等号是必要的。(感谢大卫巴克)
$app_id = APP_ID;
$canvas_page = APP_LINK;
$auth_url = "http://www.facebook.com/dialog/oauth?client_id=". $app_id . "&redirect_uri=" . urlencode($canvas_page) . "&scope=email,read_stream";
echo "<script> top.location.href='" . $auth_url . "';</script>";
当你这样做时你会得到什么:echo "<script> alert('".$auth_url."');</script>";
?
此外,在 PHP 中执行此操作的更简单方法如下:
$app_id = APP_ID;
$canvas_page = APP_LINK;
$auth_url = "http://www.facebook.com/dialog/oauth?client_id". $app_id . "&redirect_uri=" . urlencode($canvas_page) . "&scope=email,read_stream";
echo <<<EOT
<script>
top.location.href="$auth_url";
//checking the value of auth_url to see if that is the reason redirect is not happening.
if (window.console) {
console.log("$auth_url");
} else {
alert("$auth_url");
}
</script>
EOT;
使用 heredoc 语法,您不必转义任何内容,您可以在其中使用单引号和双引号,并且变量在您的字符串中本质上被替换。;)