PHP + Facebook: how to upload photo on the wall?

非 Y 不嫁゛ 提交于 2019-12-11 01:57:00

问题


The usual way as I post messages to my page wall is like:

        $args = array(
            'access_token'  => $page_access_token,
            'message'       => $title,
            'link'          => $link,
            'name'          => 'This is title',
            'description'   => 'This is a testing of the posting on page',
            //'picture'     => 'http://www.example.com/directory/images/30.jpg'
        );

        $post_id = $facebook->api("/$pageId/feed","post",$args);

But how can I post an images to my wall - the alternative to: click to UPLOAD button, pick out an image -> and upload, image is on to wall.

I have on my FTP some images and these ones I would like to upload to my wall.

Thanks in advance


回答1:


https://developers.facebook.com/blog/post/498/ this link can help you...

Here are some various ways to upload photos using the Graph API. The examples assume you've instantiated the $facebook object and have a valid session.

1 - Default Application Album of Current User This example will upload the photo to your default application album of the current user. If the album does not yet exist it will be created.

$facebook->setFileUploadSupport(true); $args = array('message' => 'Photo Caption'); $args['image'] = '@' . realpath($FILE_PATH);

$data = $facebook->api('/me/photos', 'post', $args); print_r($data); 2 - Target Album` This example will upload the photo to a specific album.

$facebook->setFileUploadSupport(true); $args = array('message' => 'Photo Caption'); $args['image'] = '@' . realpath($FILE_PATH);

$data = $facebook->api('/'. $ALBUM_ID . '/photos', 'post', $args); print_r($data);




回答2:


I'm NOT using the Facebook API include, so i'm trying to integrate Facebook stuff using only curl requests directly to the Graph API URL.

So, to post images in the User's wall, the simplest way I found is just to indicate the Image URL.

<?php
$url = "https://graph.facebook.com/me/photos?access_token=" . $FacebookToken;
$url = $url . "&url=" . urlencode("http://www.url.to/the/image.jpg");
$url = $url . "&message=" . urlencode($Description);
$url = $url . "&method=POST";

$data = file_get_contents($url); // Can change to curl if
                                 // file_get_contents is blocked on your host
?>



回答3:


Here is a short example, recovered from my codes:

$fbPost = curl_init();
curl_setopt($fbPost, CURLOPT_RETURNTRANSFER, true);
curl_setopt($fbPost, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($fbPost, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($fbPost, CURLOPT_POST, true);
curl_setopt($fbPost, CURLOPT_ENCODING, 'gzip');

$photoInfo = array(
    'access_token' => <USER_ACCESS_TOKEN>,
    'name' => <IMAGE_DESCRIPTION>,
    'url' => <ABSOLUTE_URL>/images/photo.jpg',
);

curl_setopt($fbPost, CURLOPT_URL, 'https://graph.facebook.com/<USER_ID>/photos');
curl_setopt($fbPost, CURLOPT_POSTFIELDS, $photoInfo);
$result = json_decode(curl_exec($fbPost), true);
curl_close($fbPost);


来源:https://stackoverflow.com/questions/13407800/php-facebook-how-to-upload-photo-on-the-wall

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!