Vehlad
Vehlad

Reputation: 155

Facebook PHP SDK Upload Images - Path of Image

providing option on website to click and upload image on facebook: Got this code after search

$pic = $_GET['PhotoID']; //example: http//www.mysite.com/content/2013/03/image.jpg
copy($pic, 'tmp/file.jpg');
$args['image'] = '@' . realpath('tmp/file.jpg');
$publish = $facebook->api('/me/photos', 'post', $args);
unlink('tmp/file.jpg');

This code is working fine. But Copying image to tmp take some time I want to speed up the process. (uploading images from my website only no external image)

How can I use this code without copying to temp:

 $pic = $_GET['PhotoID']; //example: http://www.mysite.com/content/2013/03/image.jpg
    $args['image'] = '@' . realpath('$pic');
    $publish = $facebook->api('/me/photos', 'post', $args);

not sure about usage of '@' & realpath Can i use full image url without using realpath:

`$args['image'] = $pic;
        $publish = $facebook->api('/me/photos', 'post', $args);`

& what is the best way to show error message if user dnt authorize the application. or errors like Fatal error: Uncaught OAuthException occurs

Upvotes: 1

Views: 3280

Answers (2)

Smita
Smita

Reputation: 4634

You don't need to copy image to /tmp. you can directly upload image by providing image url itself in parameter. the below code should work:

$picUrl = 'http//www.mysite.com/content/2013/03/image.jpg';
$photoId = $facebook->api("me/photos","POST",array('url'=>$picUrl,'message'=>"status message")

Upvotes: 2

kumar
kumar

Reputation: 1826

I've done the same in my app,jst go with this.. make sure that you have the permission publish_stream

$attachment = array(
                'message' => 'Message',
                'name' =>'your app name',
                'caption' => "caption under the image",
                'link' => 'your link',
                'description' => 'description regarding the image',
                'picture' => 'path of the picture(doesnt work for the local path, should be server path)',
                'method'=>'stream.publish',
                'actions' => array(
                                array(
                                  'name' => 'App name',
                                  'link' => 'your app link'
                                )
                            )
                );

then after

$result = $facebook->api('/'.$uid.'/feed/','post',$attachment);

here $uid is your user id of the facebook, which you can get from the facebook config file

$facebook = new Facebook('configure your facebook config file here');    
$my_details = $facebook->api('/me');
$uid = $my_details['id'];  // id of the user

Upvotes: 0

Related Questions