PHP脚本也应该上传图像及其描述
下面是我的脚本,我想用它来更新我的facebook fanpage上的状态,但它的工作方式不允许上传的图像在facebook fanpage的图库中查看。请查看此链接()这是一个详细的屏幕截图,显示了我的脚本正在发生什么,以及我试图从脚本中做什么/想要什么。请让我知道我的脚本有什么问题,我应该怎么做PHP脚本也应该上传图像及其描述,php,facebook,facebook-graph-api,facebook-php-sdk,Php,Facebook,Facebook Graph Api,Facebook Php Sdk,下面是我的脚本,我想用它来更新我的facebook fanpage上的状态,但它的工作方式不允许上传的图像在facebook fanpage的图库中查看。请查看此链接()这是一个详细的屏幕截图,显示了我的脚本正在发生什么,以及我试图从脚本中做什么/想要什么。请让我知道我的脚本有什么问题,我应该怎么做 <?php require 'src/facebook.php'; $app_id = "332267477347"; $app_secret = "xxxxxxxx"; $faceb
<?php
require 'src/facebook.php';
$app_id = "332267477347";
$app_secret = "xxxxxxxx";
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true,
'fileUpload' => true,
));
$facebook->setFileUploadSupport(true);
$user = $facebook->getUser();
//echo $user;
if(($facebook->getUser())==0)
{
header("Location:{$facebook->getLoginUrl(array('scope' => 'photo_upload,user_status,publish_stream,user_photos,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))
{
$folder = "pak/".$_FILES['pic']['name'];
$fold = 'http://snowdrop.com.pk/fb/'.$folder;
echo $fold."<br>";
if( move_uploaded_file($_FILES['pic']['tmp_name'], $folder) )
{
#Upload photo here
$img = realpath($_FILES["pic"]["tmp_name"]);
$attachment = array('message' => $_POST['textfield'],
'picture' => $fold,
'access_token' => $access_token,
);
$status = $facebook->api("/194458563914948/feed", 'post', $attachment);
echo $status;
var_dump($status);
}
else{
echo 'Only jpg, png and gif image types are supported!';
}
}
?>
<body>
<!-- Form for uploading the photo -->
<div class="main">
<p>Select a photo to upload on Facebook Fan Page</p>
<form method="post" action="" enctype="multipart/form-data">
<p>Select the image: <input type="file" name="pic" />
<br />
<label>Description
<input type="text" name="textfield" id="textfield" />
</label>
</p>
<p><input class="post_but" type="submit" value="Upload to my album" /></p>
</form>
</div>
</body>
要获得想要的效果,您需要上传到相册id。在下面的代码中,只需添加您希望上传到的相册id即可
如何发出HTTP POST请求,使用发布流对页面ID/照片进行发布,并管理页面权限。如果($facebook->getUser())==0){header(“位置:{$facebook->getLoginUrl(数组('scope'=>'photo\u upload,user\u status,publish\u stream,user\u photos,manage\u pages'))};退出;}问题正在解决,现在这行代码“source'=>@'有问题$img,当我将其更改为“source”=>“@”时。realpath($folder),然后代码就像一个符咒一样工作。。。。
$status = $facebook->api("/Album_ID/photos", 'post', $attachment);
<?php
require 'src/facebook.php';
$app_id = "332267477347";
$app_secret = "xxxxxxxx";
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true,
'fileUpload' => true,
));
$facebook->setFileUploadSupport(true);
$user = $facebook->getUser();
//echo $user;
if(($facebook->getUser())==0)
{
header("Location:{$facebook->getLoginUrl(array('scope' => 'photo_upload,user_status,publish_stream,user_photos,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))
{
$folder = "pak/".$_FILES['pic']['name'];
$fold = 'http://snowdrop.com.pk/fb/'.$folder;
echo $fold."<br>";
if( move_uploaded_file($_FILES['pic']['tmp_name'], $folder) )
{
#Upload photo here
$img = realpath($_FILES["pic"]["tmp_name"]);
$attachment = array('message' => $_POST['textfield'],
'source' => $fold,
'access_token' => $access_token,
);
$status = $facebook->api("/Album_ID/photos", 'post', $attachment);
echo $status;
var_dump($status);
}
else{
echo 'Only jpg, png and gif image types are supported!';
}
}
?>
<body>
<!-- Form for uploading the photo -->
<div class="main">
<p>Select a photo to upload on Facebook Fan Page</p>
<form method="post" action="" enctype="multipart/form-data">
<p>Select the image: <input type="file" name="pic" />
<br />
<label>Description
<input type="text" name="textfield" id="textfield" />
</label>
</p>
<p><input class="post_but" type="submit" value="Upload to my album" /></p>
</form>
</div>
</body>