在我的facebook粉丝页面的墙上发布PHP脚本时出现问题,$access\u令牌的永久值
可能重复:在我的facebook粉丝页面的墙上发布PHP脚本时出现问题,$access\u令牌的永久值,facebook,facebook-graph-api,Facebook,Facebook Graph Api,可能重复: 我有这个代码,它的工作非常好,只是有一个问题,我无法将上传的图片传递到facebook->api(无论是否有效),下面的内容总是回音 echo“仅支持jpg、png和gif图像类型!”; 我甚至删除了对图像类型的检查,并尝试从中获取图像 $img=realpath($_文件[“pic”][“tmp_名称]); 但是$img什么都没有,默认情况下会在facebook上上传一张空图片作为我的页面 请检查我下面的代码,让我知道我的代码有什么问题,这样我就可以上传图片了 <?
我有这个代码,它的工作非常好,只是有一个问题,我无法将上传的图片传递到facebook->api(无论是否有效),下面的内容总是回音 echo“仅支持jpg、png和gif图像类型!”; 我甚至删除了对图像类型的检查,并尝试从中获取图像 $img=realpath($_文件[“pic”][“tmp_名称]); 但是$img什么都没有,默认情况下会在facebook上上传一张空图片作为我的页面 请检查我下面的代码,让我知道我的代码有什么问题,这样我就可以上传图片了
<?
require 'src/facebook.php';
$app_id = "364900470214655";
$app_secret = "xxxxxxxx";
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true,
'fileUpload' => true,
));
$user = $facebook->getUser();
//echo $user;
if(($facebook->getUser())==0)
{
header("Location:{$facebook->getLoginUrl(array('req_perms' => 'user_status,publish_stream,user_photos,offline_access,manage_pages'))}");
exit;
}
else {
$accounts_list = $facebook->api('/me/accounts');
echo "i am connected";
}
$valid_files = array('image/jpeg', 'image/png', 'image/gif');
//to get the page access token to post as a page
foreach($accounts_list['data'] as $account){
if($account['id'] == 194458563914948){ // my page id =123456789
$access_token = $account['access_token'];
echo "<p>Page Access Token: $access_token</p>";
}
}
//posting to the page wall
if (isset($_FILES) && !empty($_FILES))
{
if( !in_array($_FILES['pic']['type'], $valid_files ) )
{
echo 'Only jpg, png and gif image types are supported!';
}
else{
#Upload photo here
$img = realpath($_FILES["pic"]["tmp_name"]);
$attachment = array('message' => 'this is my message',
'access_token' => $access_token,
'name' => 'This is my demo Facebook application!',
'caption' => "Caption of the Post",
'link' => 'example.org',
'description' => 'this is a description',
'picture' => '@' . $img,
'actions' => array(array('name' => 'Get Search',
'link' => 'http://www.google.com'))
);
$status = $facebook->api('/194458563914948/feed', 'POST', $attachment); // my page id =123456789
var_dump($status);
}
}
?>
<body>
<!-- Form for uploading the photo -->
<div class="main">
<p>Select a photo to upload on Facebook Fan Page</p>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
<p>Select the image: <input type="file" name="pic" /></p>
<p><input class="post_but" type="submit" value="Upload to my album" /></p>
</form>
</div>
</body>
创建新相册并添加照片
这是将照片上载到相册ID/photos图形API端点的场景。此示例的用户界面允许用户在提交新照片之前选择照片并添加标题
使用PHP:
我对用户进行了检查,但有一个问题,我下面的链接没有检查是否有人登录了facebook,或者没有让我知道如何提供我甚至在我的网站上向facebook添加了登录按钮,但当有人点击它时,会出现一个错误,这是我的链接:请接受我愚蠢的问题,我是一个新手。。此链接包含facebook提供的代码,但当我点击登录时仍会出现错误,请告诉我如何检查是否有人登录facebook。因此,我可以使用下面链接中给出的代码执行所需的功能。最近的一次。。。。facebook-php-sdk-v3.1.1-0-g9513f08$uid=$facebook->getUser();echo$uid;它回声0我想我必须添加一个登录按钮在我的域一旦我登录,然后我可以得到用户id详细信息等,并能够在我的fb页面上发布,但我很惊讶,我有一个代码仍然在我的相册中发布,没有任何登录按钮在我的网站上所有它需要我当前的访问令牌,但该死的,我无法在我的fb墙上发布,如前所述请检查我上面的完整代码:我所要做的就是创建这样一个php脚本,让我可以从我的网站上传到我的facebook粉丝页面的墙上。。。谢谢
<?php
$app_id = "YOUR_APP_ID";
$app_secret = "YOUR_APP_SECRET";
$post_login_url = "YOUR_POST-LOGIN_URL";
$album_name = 'YOUR_ALBUM_NAME';
$album_description = 'YOUR_ALBUM_DESCRIPTION';
$code = $_REQUEST["code"];
//Obtain the access_token with publish_stream permission
if(empty($code))
{
$dialog_url= "http://www.facebook.com/dialog/oauth?"
. "client_id=" . $app_id
. "&redirect_uri=" . urlencode($post_login_url)
. "&scope=publish_stream";
echo("<script>top.location.href='" . $dialog_url .
"'</script>");
}
else {
$token_url= "https://graph.facebook.com/oauth/"
. "access_token?"
. "client_id=" . $app_id
. "&redirect_uri=" . urlencode( $post_login_url)
. "&client_secret=" . $app_secret
. "&code=" . $code;
$response = file_get_contents($token_url);
$params = null;
parse_str($response, $params);
$access_token = $params['access_token'];
// Create a new album
$graph_url = "https://graph.facebook.com/me/albums?"
. "access_token=". $access_token;
$postdata = http_build_query(
array(
'name' => $album_name,
'message' => $album_description
)
);
$opts = array('http' =>
array(
'method'=> 'POST',
'header'=>
'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$result = json_decode(file_get_contents($graph_url, false,
$context));
// Get the new album ID
$album_id = $result->id;
//Show photo upload form and post to the Graph URL
$graph_url = "https://graph.facebook.com/". $album_id
. "/photos?access_token=" . $access_token;
echo '<html><body>';
echo '<form enctype="multipart/form-data" action="'
.$graph_url. ' "method="POST">';
echo 'Adding photo to album: ' . $album_name .'<br/><br/>';
echo 'Please choose a photo: ';
echo '<input name="source" type="file"><br/><br/>';
echo 'Say something about this photo: ';
echo '<input name="message" type="text"
value=""><br/><br/>';
echo '<input type="submit" value="Upload" /><br/>';
echo '</form>';
echo '</body></html>';
}
?>