我有一个使用Facebook PHP API的应用程序。这是我的代码的一部分,我不确定我做错了什么或者我现在缺少什么。我似乎无法在Facebook上的用户时间轴上发布图像。任何帮助将非常感激。非常感谢。
<?php
echo "<p> Hello World!</p>";
// php 5.3 and up can throw an error if this is not set
date_default_timezone_set("Europe/London");
// much of the example code on the web forgets to include these HttpClients, for some reason
require_once( '../docs/Facebook/HttpClients/FacebookHttpable.php' );
require_once( '../docs/Facebook/HttpClients/FacebookCurl.php' );
require_once( '../docs/Facebook/HttpClients/FacebookCurlHttpClient.php' );
require_once( '../docs/Facebook/FacebookSession.php' );
require_once( '../docs/Facebook/FacebookRedirectLoginHelper.php' );
require_once( '../docs/Facebook/FacebookRequest.php' );
require_once( '../docs/Facebook/FacebookResponse.php' );
require_once( '../docs/Facebook/FacebookSDKException.php' );
require_once( '../docs/Facebook/FacebookRequestException.php' );
require_once( '../docs/Facebook/FacebookAuthorizationException.php' );
require_once( '../docs/Facebook/GraphObject.php' );
// This one is also often left out
require_once( '../docs/Facebook/Entities/AccessToken.php');
// store your $HOSTNAME, $APPID and $SECRET in this file:
require_once( '../docs/my_app_credentials.php' );
session_start();
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
// init app with app id (APPID) and secret (SECRET)
FacebookSession::setDefaultApplication($APPID,$SECRET);
// login helper with redirect_uri
$PAGENAME="page2_upload_photo.php";
$REDIRECT_URL="http://".$HOSTNAME.'/'.$PAGENAME;
$IMAGE_FILENAME='@images/Flower.jpg';
$IMAGE_MESSAGE='hello flower';
$helper = new FacebookRedirectLoginHelper( $REDIRECT_URL );
echo "<p> the redirect url is ".$REDIRECT_URL."</p>";
try {
// echo "<p> about to try to get session: the helper variable: </p>";
// var_dump($helper);
$session = $helper->getSessionFromRedirect();
// echo "<p> the session variable:</p>";
// var_dump($session);
}
catch( FacebookRequestException $ex ) {
// When Facebook returns an error
echo "<p> There was a facebook request exception</p>";
}
catch( \Exception $ex ) {
echo "<p> There was a validation failure</p>";
// When validation fails or other local issues
}
// see if we have a session
if ( isset( $session ) ) {
echo "<p> Now try to upload a photo to the album /me</p>";
try {
// Upload to a user's profile. The photo will be in the
// first album in the profile. You can also upload to
// a specific album by using /ALBUM_ID as the path
//$myCurlFile = new CURLFile('@./'.$IMAGE_FILENAME, 'image/jpg');
$myPhoto = "@images/Flower.jpg";
//var_dump($myCurlFile);
$response = (new FacebookRequest(
$session, 'POST', '/me/photos',
array(
'source'=>'$IMAGE_FILENAME',
'message'=>'$IMAGE_MESSAGE'
)
))->execute()->getGraphObject();
// If you're not using PHP 5.5 or later, change the file reference to:
// 'source' => '@/path/to/file.name'
echo "Posted with id: " . $response->getProperty('id');
} catch(FacebookRequestException $e) {
echo "Exception occurred, code: " . $e->getCode();
echo " with message: " . $e->getMessage();
}
} else {
// show login url
echo '<a href="' . $helper->getLoginUrl($scope) . '">Login</a>';
}
?>
答案 0 :(得分:0)
您尚未设置上传文件。
$response = (new FacebookRequest(
$session, 'POST', '/me/photos',
array(
'source'=>'$IMAGE_FILENAME',
'message'=>'$IMAGE_MESSAGE'
)
))->execute()->getGraphObject();
您将变量括在引号中,这将使它成为一个字符串,因此source获取文字字符串$ IMAGE_FILENAME
这是一个简单的例子
$IMAGE_FILENAME='@images/Flower.jpg';
$IMAGE_MESSAGE='hello flower';
$arr = array(
'source'=>'$IMAGE_FILENAME',
'message'=>'$IMAGE_MESSAGE'
);
$correct_arr = array(
'source'=>$IMAGE_FILENAME,
'message'=>$IMAGE_MESSAGE
);
print_r($arr);
print_r($correct_arr);
响应
Array
(
[source] => $IMAGE_FILENAME
[message] => $IMAGE_MESSAGE
)
Array
(
[source] => @images/Flower.jpg
[message] => hello flower
)