简体   繁体   中英

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

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

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);

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
?>

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM