如果我直接传递数据变量,此代码有效,但我想要来自用户输入字段的数据
$ data = [“message”=> “测试帖子”];
$ profile_request = $ fb-> post('/ me / feed',$ data);
上面的代码工作,如果我从POST方法得到它不工作 任何建议都将不胜感激,
index.php
<?php
require_once 'appconfig.php';
$permissions = ['email','publish_actions'];
$loginUrl = $helper->getLoginUrl('http://localhost/Api-Integeration/fbpost/logged.php', $permissions);
//echo '<a href="' . $loginUrl . '">Post to Facebook!</a>';
?>
<!DOCTYPE html>
<html>
<head>
<title>Post to user timeline</title>
</head>
<body>
<form action="<?php echo $loginUrl; ?>" method="POST" accept-charset="utf-8">
<textarea rows="6" name="message" style="width:40%;" placeholder="Enter Post Data"></textarea>
<br />
<input type="submit" value="POST TO FACEBOOK" name="submit">
</form>
</body>
</html>
logged.php
<?php
require_once 'appconfig.php';
try {
if (isset($_SESSION['facebook_access_token'])) {
$accessToken = $_SESSION['facebook_access_token'];
} else {
$accessToken = $helper->getAccessToken();
}
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
if (isset($accessToken)) {
if (isset($_SESSION['facebook_access_token'])) {
$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
} else {
// getting short-lived access token
$_SESSION['facebook_access_token'] = (string) $accessToken;
// OAuth 2.0 client handler
$oAuth2Client = $fb->getOAuth2Client();
// Exchanges a short-lived access token for a long-lived one
$longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['facebook_access_token']);
$_SESSION['facebook_access_token'] = (string) $longLivedAccessToken;
// setting default access token to be used in script
$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
}
// redirect the user back to the same page if it has "code" GET variable
if (isset($_GET['code'])) {
header('Location: logged.php');
}
/*user messaegs*/
$usermsg = $_POST['message'];
$data = ["message" => $usermsg];
try {
$profile_request = $fb->post('/me/feed', $data);
$user = $profile_request->getGraphUser();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
session_destroy();
// redirecting user back to app login page
header('Location: ./');
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
// Now you can redirect to another page and use the access token from $_SESSION['facebook_access_token']
} else{
print_r($_POST);
echo "not set";
}
?>