Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php中,页面未正确使用标头重定向_Php_Facebook_Loops_Redirect_Infinite - Fatal编程技术网

在php中,页面未正确使用标头重定向

在php中,页面未正确使用标头重定向,php,facebook,loops,redirect,infinite,Php,Facebook,Loops,Redirect,Infinite,我不知道标题位置部分出了什么问题,但是有一个错误,那就是页面会无限重定向并在一段时间后停止。这是我的代码,当我选择一张图片时,它应该重定向到facebook制作封面或制作个人资料页面,但它会通过循环继续重定向 <?php include_once("inc/facebook.php"); //include facebook api library ######### edit details ########## $appId = ''; //Facebook App ID $appS

我不知道标题位置部分出了什么问题,但是有一个错误,那就是页面会无限重定向并在一段时间后停止。这是我的代码,当我选择一张图片时,它应该重定向到facebook制作封面或制作个人资料页面,但它会通过循环继续重定向

<?php
include_once("inc/facebook.php"); //include facebook api library

######### edit details ##########
$appId = ''; //Facebook App ID
$appSecret = ''; // Facebook App Secret
$return_url = 'http://www.mysite.com/testing/process.php';  //return url (url to script)
$homeurl = 'http://www.mysite.com/testing';  //return to home
$fbPermissions = 'publish_stream,user_photos';  //Required facebook permissions
##################################



$GetPicId = $_GET["pid"]; // Picture ID from Index page
$PicLocation ='';

/*
Users do not need to know original location of image.
I think it's better to get image location from database using ID.
for demo here i'am using PHP switch.
*/
switch($GetPicId)
{
case 1:
    $PicLocation = 'cover_pics/cover1.jpg';
    break;
case 2:
    $PicLocation = 'cover_pics/cover2.jpg';
    break;
case 3:
    $PicLocation = 'cover_pics/cover3.jpg';
    break;
default:
    header('Location: ' . $homeurl);
    break;
}

//Call Facebook API
$facebook = new Facebook(array(
'appId'  => $appId,
'secret' => $appSecret,
'fileUpload' => true,
'cookie' => true
));

//get user
$fbuser = $facebook->getUser();
$access_token = $facebook->getAccessToken();

//variables we are going to post to facebook
$msg_body = array(
'access_token' => $access_token,
'message' => 'I liked this pic from '. $homeurl .' it is perfect for my cover photo.',
'source' => '@'.realpath($PicLocation)
);

if ($fbuser){ //user is logged in to facebook, post our image
try {
$uploadPhoto = $facebook->api('/me/photos', 'post', $msg_body );
} catch (FacebookApiException $e) {
echo $e->getMessage(); //output any error
}
}else{
$loginUrl =   $facebook->getLoginUrl(array('scope'=>$fbPermissions,'return_url'=>$return_url));
header('Location: ' . $loginUrl);
}

if($uploadPhoto)
{
/*
image is posted in user facebook account, but still we need to send user to facebook
so s/he can set cover or profile picture!
*/

//Get url of the picture just uploaded in user facebook account
$jsonurl = "https://graph.facebook.com/".$uploadPhoto["id"]."&?access_token=".$access_token;
$json = file_get_contents($jsonurl,0,null,null);
$json_output = json_decode($json);

/*
We can not set facebook cover or profile picture automatically yet,
So, the trick is to post picture into user facebook account first
and then redirect them to a facebook profile page where they just have to click a button to set it.
*/
echo '<html><head><title>Update Image</title>';
echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
echo '<link href="style.css" rel="stylesheet" type="text/css" />';
echo '</head><body>';
echo '<div align="center" class="fbpicwrapper">';
echo '<h1>Image is sent to your facebook account!</h1>';
echo '<div class="fbpic_desc">Click on desired button you want to do with this image!</div>';
echo '<div class="option_img"><img src="'.$json_output->source.'" /></div>';

/*
Links (buttons) below will send user to facebook page,
where they just need to crop or correct propertion of image and hit apply button.
*/
echo '<a class="button" target="_blank" href="http://www.facebook.com/profile.php?preview_cover='.$uploadPhoto["id"].'">Make Your Profile Cover</a>';
echo '<a class="button" target="_blank" href="http://www.facebook.com/photo.php?fbid='.$uploadPhoto["id"].'&type=1&makeprofile=1&makeuserprofile=1">Make Your Profile Picture</a>';
echo '<a class="button" href="'.$homeurl.'">Back to main Page.</a>';
echo '</div>';
echo '</body></html>';
}

?>

在使用
标题(位置…
重定向之前,请删除任何
回显
或打印到输出中的任何空白。在您已经开始打印输出后,重定向将不起作用


方法是将要打印的所有内容保存为字符串,以防没有重定向,并仅在最后打印到屏幕。

您在两行中使用了
header
方法,该方法可以在两次内按某些真实条件运行。它可以形成一个循环

如果
开关
不匹配任何情况,并且
$fbuser
同时等于

第一个
标题
方法:

default:
    header('Location: ' . $homeurl);
    exit(); //added
    break;
if ($fbuser){ 
    //user is logged in to facebook, post our image
}
else {
    $loginUrl = $facebook->getLoginUrl(array('scope' => $fbPermissions, 'return_url' => $return_url));
    header('Location: ' . $loginUrl);
    exit(); //added
其他
标题
方法:

default:
    header('Location: ' . $homeurl);
    exit(); //added
    break;
if ($fbuser){ 
    //user is logged in to facebook, post our image
}
else {
    $loginUrl = $facebook->getLoginUrl(array('scope' => $fbPermissions, 'return_url' => $return_url));
    header('Location: ' . $loginUrl);
    exit(); //added

那么,我应该删除哪个标头方法才能使其正常工作?@RohanMishra add
exit()
在每个
标题之后
方法,以确保代码的运行过程不会继续,然后再试一次。或者您在调用Facebook API时遇到了问题?这可能会形成一个循环。可能存在什么问题?当我运行应用程序时,第一次它在登录后获取了权限,然后它一直重定向,现在我只是登录,它一直重定向,所以,很难理解。我建议
var\u dump()
您的变量和
die()
调试页面。