使用php SDK发布时使用带链接帖子的缩略图时出现无效文件错误

时间:2014-02-03 11:09:57

标签: php facebook sdk

我正在尝试使用php sdk上传带链接帖子的自定义缩略图,但它显示错误

(#100)文件无效。预期的以下类型之一的文件:image / jpg,image / jpeg,image / gif,image / png

上传的文件可以发布照片,所以我知道文件没问题,尝试发布链接时可能会出现什么问题?

在此页https://developers.facebook.com/docs/reference/ads-api/link_post_custom_image/

它表明它应该有效。我已经尝试忽略上传的文件并硬编码已知现有图像的路径,但在尝试发布带有'thumbnail'参数的链接时仍然显示错误

我已尝试先将文件复制到本地,然后使用该路径,但仍显示错误

如果我省略缩略图和图片参数,facebook会抓取它自己的缩略图图像,这样发布功能就可以了,而不是缩略图参数

这是一个代码示例 html表单

<form id="form" method="post" action="newpost.php" enctype="multipart/form-data">
    <div id="container">
        <div id="content">
            <div id="fbpages">
                <?php
                foreach($c->accounts as $page){
                    echo '<li title="Post to '.$page->name.'" class="fbpage" pageid="'.$page->id.'"><img src="http://graph.facebook.com/'.$page->id.'/picture" width="37" height="37"/></li>';
                }
                echo '<li title="post to personal timeline" class="fbpage" pageid="personal"><img src="http://graph.facebook.com/'.$user.'/picture" width="37" height="37"/></li>';
                echo '<li title="Post to Wordpress" class="fbpage" pageid="wordpress"><img src="images/wordpress-logo.png"/></li>';
                ?>  
                <input type="hidden" name="fbpages" />          
            </div>
            <div id="fields">
                <div id="text-1">
                    <textarea id="message" name="message"><?php if($c->alt){ echo $c->alt;}?></textarea>
                </div>
                <div id="image">
                    <img src="<?php echo $c->src;?>" />
                    <br><input name="nopicture" value="yes" type="checkbox"/>No Picture (let facebook choose)
                    <input type="file" name="file"/>
                </div>
                <div id="text-2">
                    <p>Name<br><input type="text" id="name" name="name" placeholder="name" value="<?php echo $c->title;?>"/></p>
                    <p>Link<br><input type="text" id="link" name="link" placeholder="link" value="<?php echo $c->foundpage;?>"/></p>
                    <?php $da = parse_url($c->foundpage); $caption = $da['host'];   ?>
                    <p>Caption<br><input type="text" id="caption" name="caption" placeholder="caption" value="<?php echo $caption;?>"/></p>
                    <p>Description<br><textarea id="description" name="description" placeholder="description"><?php echo $c->description ? $c->description : $c->alt;?></textarea></p>
                </div>
            </div>
            <div id="controls">
                <div id="leftcontrols">
                    <p><strong>Auto Comment</strong><p>
                        <input type="checkbox" id="docomment" name="docomment" value="yes" />
                        <textarea id="comment" name="comment" rows="3" placeholder="Comment text"><?php echo 'Found at '.$c->foundpage;?></textarea>
                    </p>
                </div>
                <div id="rightcontrols">
                    <strong>Select type of post:</strong><br />
                    <select id="type" name="type">
                        <option value="photo">Post a Photo</option>
                        <option value="link">Post a Link</option>
                        <option value="status">Post a Status</option>
                    </select>
                    <p class="controls">
                        <strong>Time and Date to post</strong><br />
                            <input type="text" id="date" name="date" value="<?php echo date('d/m/Y H:i');?>" placeholder="date"/>
                            <br><button id="now">Now</button> <button id="plus6">+6 Hours</button>
                        </div>
                    </p>
                    <input type="submit" name="clear" value="clear"/>
                    <p id="submit">
                        <input type="submit" name="submit" value="Submit"/>

                    </p>
                </div>
            </div>
        </div>

作为照片正常发布的功能

if(!empty($_FILES["file"]["tmp_name"])){
    $facebook->setFileUploadSupport(true);
    $c['image'] = '@'.$_FILES["file"]["tmp_name"];
}
if($type == 'photo'){
    // build args
    $args = array(
        'message' => $message,
        'published'=>$scheduledpost ? 0 : 1,
        'no_story' => 0,
        'access_token' => $accesstoken
    );
    if(isset($c['image'])){
        $args['image'] = $c['image'];
    } else {
        $args['url'] = $c['src'];
    }
    if($scheduledpost){
        $args['scheduled_publish_time'] = $timetopost;
    } 

    try {
        $photo = $facebook->api($pageid . '/photos', 'post', $args);
        // post comment if sent
        if( is_array( $photo ) && !empty( $photo['id'] ) ){
            ///photo posted, make comment with link
            if(isset($_POST['docomment']) && $_POST['docomment'] == 'yes'){
                $commentmessage = 'Found at '.$_POST['comment'];
                try{
                    $comment = $facebook ->api('/'.$photo['id'].'/comments', 
                        'post', 
                        array(
                            'access_token' => $accesstoken,
                            'message' => $commentmessage,
                        )
                    );
                } catch (FacebookApiException $e){
                    echo '<h2>Had an error updating the comment '.$e->getMessage();
                }
            }

        }
    }catch(FacebookApiException $e){
        echo "<h2>Error</h2>can't post photo. It must be protected from being shared. <script>setTimeout(\"self.close();\",2500);</script>";
        exit;
    }

但是当我尝试使用相同的上传文件发布链接时,会显示错误

if($type == 'link'){
    //debugbreak();

    $args = array(
        'access_token' => $accesstoken,
        'message' => $message,
        'name' => $_REQUEST['name'],
        'link' => $_REQUEST['link'],
        'caption'=>$_REQUEST['caption'],
        'description' => $_REQUEST['description'],
        'picture'=> $c['src'],
        'published'=> 1
    );
    if(isset($_POST['nopicture'])){
        unset($args['picture']);
    }
    if(isset($c['image'])){
        $args['thumbnail'] = $c['image'];
    } 
    if($scheduledpost){
        $args['scheduled_publish_time'] = $timetopost;
        $args['published'] = 0 ;
    }

    try{                           
        $link = $facebook->api('/'.$pageid.'/feed','POST',$args);
        if( is_array( $link ) && !empty( $link['id'] ) ){
            ///photo posted, make comment with link
            if(isset($_POST['docomment']) && $_POST['docomment'] == 'yes'){
                $commentmessage = 'Found at '.$_POST['comment'];
                try{
                    $comment = $facebook ->api('/'.$link['id'].'/comments', 
                        'post', 
                        array(
                            'access_token' => $accesstoken,
                            'message' => $commentmessage,
                        )
                    );
                } catch (FacebookApiException $e){
                    echo '<h2>Had an error updating the comment '.$e->getMessage();
                }
            }

        }
    } catch(FacebookApiException $e){
        echo "<h2>Error</h2>can't post link.<script>setTimeout(\"self.close();\",2500);</script>";
        exit;
    }

更新

我最终将图像作为nostory发送到Facebook,然后从信息中获取源代码并将其用作链接的src

if(isset($c['image'])){

        $targs = array(
           'published'=> 0,
           'no_story' => 0,
           'access_token' => $accesstoken,
           'image'=>$c['image']  
        );
        //try temporary photo
        $picture = $facebook->api($pageid . '/photos', 'post', $targs);
        if($picture){
            // get info about picture and set src to source 
            $picinfo = $facebook->api($picture['id']);
            $args['picture'] = $picinfo['source'];
            $c['src'] = $args['picture'];
        }
    } 

0 个答案:

没有答案