将上传的图像传递给facebook-> api函数的PHP SCRIPT问题 [英] PHP SCRIPT issue with passing the uploaded image to facebook->api function

查看:101
本文介绍了将上传的图像传递给facebook-> api函数的PHP SCRIPT问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码,它的工作很好,如果我提供一个图像的直接链接。






只是一个问题,我无法将我上传的图片传递给Facebook-> api(无论是否有效或者没有),并且以下总是回显






echo'只支持jpg,png和gif图像类型!'; / p>




我甚至删除了对图像类型的检查,并尝试从
$ img = realpath($ _FILES [ PIC] [ tmp_name的值]);
但$ img没有任何内容,并在Facebook上默认上传一个空的图像作为我的页面
请仔细检查我的以下代码,让我知道我的代码有什么问题,我该怎么办,而不是上传图像



以下代码的在线链接: http: //radiations3.com/facebook/1.php

  < 


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'))});
退出;
}
else {
$ accounts_list = $ facebook-> api('/ me / accounts');
echo我连接;
}
$ valid_files = array('image / jpeg','image / png','image / gif');


//获取页面访问令牌作为页面发布
foreach($ accounts_list ['data'] as $ account){
if($ account ['id'] == 194458563914948){//我的页面id = 123456789
$ access_token = $ account ['access_token'];
echo< p>页面访问令牌:$ access_token< / p>;
}
}

//发布到页面墙

if(isset($ _ FILES)&&!empty($ _ FILES) )
{
if(!in_array($ _ FILES ['pic'] ['type'],$ valid_files))
{
echo'只有jpg,png和gif图像类型被支持!
}
else {
#上传照片
$ img = realpath($ _ FILES [pic] [tmp_name]);
$ attachment = array('message'=>'这是我的消息',
'access_token'=> $ access_token,
'name'=>'这是我的演示Facebook应用程序!',
'caption'=>帖子标题,
'link'=>'example.org',
'description'=>'是一个描述',
'picture'=>'@'$ img,
'actions'=> array(array('name'=>'Get Search',
'link'=>'http://www.google.com'))
);
$ status = $ facebook-> api('/ 194458563914948 / feed','POST',$ attachment); //我的页面id = 123456789
var_dump($ status);
}
}
?>
< body>
<! - 上传照片的表单 - >
< div class =main>
< p>选择要在Facebook上上传的照片粉丝专页< / p>
< form method =postaction =<?php echo $ _SERVER ['PHP_SELF'];?> ENCTYPE = 多部分/格式数据 >
< p>选择图像:< input type =filename =pic/>< / p>
< p>< input class =post_buttype =submitvalue =上传到我的相册/>< / p>
< / form>
< / div>
< / body>


解决方案

上传时,我收到以下回调


我是connectedarray(1){[id] => string(31)
194458563914948_415487041812098}


图表api资源管理器建议上传成功



https://developers.facebook.com/tools/explorer?method=GET&path=194458563914948_415487041812098


I have this code and its working awsomely fine if i provide a direct link of an image.


Just and just only one problem i am not able to pass my uploaded image to facebook->api (whether its valid or not) and the following always echo


echo 'Only jpg, png and gif image types are supported!';


i even remove the check on image type and try to get the image from $img = realpath($_FILES["pic"]["tmp_name"]); but $img gets nothing in it and uploads a by default empty image on facebook as my page Kindly check my following code and let me know what is wrong with my code and what should i do instead to upload images

Online link of the following CODE: http://radiations3.com/facebook/1.php

 <?


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>

解决方案

Upon uploading, i received the following callback

i am connectedarray(1) { ["id"]=> string(31) "194458563914948_415487041812098" }

graph api explorer suggests the upload was successful

https://developers.facebook.com/tools/explorer?method=GET&path=194458563914948_415487041812098

这篇关于将上传的图像传递给facebook-&gt; api函数的PHP SCRIPT问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆