PHP - Upload a web hosted photo to facebook album via Graph API - php

I'm trying to upload www hosted (e.g. http://www.google.se/intl/en_com/images/srpr/logo1w.png) files to a facebook album.
Creating an album works just fine, but I don't seem to uploading any photos. I'm using the facebook php-sdk ( http://github.com/facebook/php-sdk/ ) and the examples I already tried are:
Upload Photo To Album with Facebook's Graph API
How can I upload photos to album using Facebook Graph API
I'm guessing CURL uploads perhaps only can manage locally stored files and not web hosted ones.
Here's my code:
/*
Try 1:
$data = array();
$data['message'] = $attachment->post_title;
$data['file'] = $attachment->guid;
try {
$photo = $facebook->api('/' . $album['id'] . '/photos?access_token=' . $session['access_token'], 'post', $data);
} catch (FacebookApiException $e) {
error_log($e);
}
*/
// Try 2:
//upload photo
$file = $attachment->guid;
$args = array(
'message' => 'Photo from application',
);
$args[basename($file)] = '#' . realpath(file_get_contents($file));
$ch = curl_init();
$url = 'https://graph.facebook.com/' . $album['id'] . '/photos?access_token=' . $session['access_token'];
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
$data = curl_exec($ch);
//returns the photo id
print_r(json_decode($data,true));
...where attachment->guid contains the photo url.
I'm pretty much stuck right now...

i think the problem is here:
$args[basename($file)] = '#' . realpath(file_get_contents($file));
since you want to post a picture from another source (right?), you should save it temporarily on your own host.
i also needed to do something like this, and since i had to process the image, i used the following way:
$im = #imagecreatefrompng('http://www.google.se/intl/en_com/images/srpr/logo1w.png');
imagejpeg($im, 'imgs/temp/temp.jpg', 85);
$args['image'] = '#' . realpath('imgs/temp/temp.jpg');
the rest looks fine though

I'll suggest to use the Facebook php SDK, it will be easier and the code will work with future updates of the APIs:
Using the Graph API php sdk:
$fbk = new Facebook(/* conf */);
$fbk->setFileUploadSupport(true);
//If you are executing this in a script, and not in a web page with the user logged in:
$fbk->setAccessToken(/* access token from other sources */);
//To add to an album:
$fbk->api("/$albumId/photos", "POST",
array('source' => '#'. realpath($myPhoto), 'message' => "Nice photo"));
//To upload a photo directly (the album will be created automatically):
$fbk->api("/me/photos", "POST",
array('source' => '#'. realpath($myPhoto), 'message' => "Nice photo"));
Using cURL directly:
If your really want to use cURL, your code is almost correct, but the error is in the $args array:
$args = array(
'message' => 'Photo from application',
'source' => file_get_contents($file)
);
Since the key for the photo data is source, see the Facebook Doc
Note on the # in cURL:
Also notice that the # in cUrl means that the parameter will be replaced with the actual bytes of the file that follows the #, so it isn't required if you already put in the source parameter the actual bytes.

I'm guessing CURL uploads perhaps only can manage locally stored files and not web hosted ones.
No, that’s not the case.
But you need to give the full, publicly reachable HTTP URL to the image, without an # in front – and you have to use the parameter name url for this value.
https://developers.facebook.com/docs/reference/api/photo/:
You can also publish a photo by providing a url param with the photo's URL.

Related

php telegram sendPhoto not working (url & file location)

I need some help if possible with php sendPhoto api, I've been using the sendPhoto method in php on my apache server to auto send images into telegram, I've been using this same method for almost 6-7 months and from few days ago suddenly the api method stopped working. I tried passing photo= using the absolute path of file in url and in php using the files directory+filename but sends me an error msg from the api as shown below, first part is my php method which doesnt return any errors, just shows blank
# my php telegram code
$dir = "Attachments/2022/04/09/imagename.jpeg";
$chat_id = '(groupchatid)';
$bot_url = "https://api.telegram.org/bot(mybotapi)/";
$url = $bot_url . "sendPhoto?chat_id=" . $chat_id ;
$post_fields = array('chat_id' => $chat_id,
'photo' => new CURLFile(realpath($dir)),
'caption' =>'Test Image', );
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array( "Content-Type:multipart/form-data" ));
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
$output = curl_exec($ch);
When i execute this script as it used to work before recently this is the response i get from the API
{
"ok": false,
"error_code": 400,
"description": "Bad Request: invalid file HTTP URL specified: Unsupported URL protocol"
}
If I replace the image URL to another server it send the image successfully, but im unable to send anything only from my server, If I try access the file directly using the URL of my servers image file I can access it from any pc no issue, only problem is telegram fetching the image, please help, appreciate it
Excuse, I don't usually use curl, so I can give you another option:
function sendPhoto($id, $photo, $text = null){
GLOBAL $token;
$url = 'https://api.telegram.org/bot'.$token."/sendPhoto?
chat_id=$id&photo=$photo&parse_mode=HTML&caption=".urlencode($text);
file_get_contents($url);
}
Just declare the sendPhoto function in this way, put the variabile in which you stored the token instead of "$token" and use the parameters in this way:
$id = the id of the user (the one you declared like this: $id = $update['message']['from']['id'];)
$photo = absolute path of the image you want to send
$text = OPTIONAL caption for the image

Is there a simple way to verify if a file has been uploaded correctly to Amazon S3?

I use the tpyo Amazon S3.php class from https://github.com/tpyo/amazon-s3-php-class to upload files like this:
# START AMAZON S3
$s3 = new S3('KEY', 'SECRET');
$uploadfile = 'user-' . $uid . '/' . $new_file;
S3::putObject(
S3::inputFile('/home/username/www/images/'.$uid.'/'.$file_name, false),
'bucketname',
$uploadfile,
S3::ACL_PUBLIC_READ,
array(),
array(),
S3::STORAGE_CLASS_STANDARD
);
#END AMAZON S3
I've noticed that out of maybe 10,000 files that are uploading, it fails to upload maybe 10 or 20 for various reasons like network problems, server problems, etc. Is there any way to easily verify if the file has been uploaded correctly?
The solution I found is to grab the image with a class like this:
function ranger($url){
$headers = array(
"Range: bytes=0-983040"
);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($curl);
curl_close($curl);
return $data;
}
And then check the dimensions of the image and know it exists if the width and the height are >1, but this method is really slow especially for big images.
I see no class built into the tpyo S3.php file to verify if the file exists. Anybody has a better idea?
Thanks :)
I'm so stupid. There's an option called getObjectInfo.

Facebook PHP upload file from memory

I'm reading an image from my S3 bucket in AWS and want to upload it to Facebook.
This is the reading function:
/**
* Get a file from the s3 storage
*/
private function uploadPicture() {
$picture = new FacebookPicture();
$file = $this->s3Manager->getFile($this->subEndPoint,$this->verb);
$picture->pictureContent = $file["Body"];
$facebookAlbum = new FacebookAlbum();
$facebookAlbum->album = new Album();
$facebookAlbum->album->facebookAccessToken = "myAccessToken";
$facebookAlbum->id = "myAlbumId";
$facebookManager = new FacebookManager();
$facebookManager->uploadPicture($facebookAlbum,$picture);
}
This is the uploading to Facebook function
/**
* #param $facebookAlbum FacebookAlbum the album to upload the picture to
* #param $picture FacebookPicture the picture to upload
*/
public function uploadPicture($facebookAlbum,$picture)
{
$this->facebook->setAccessToken($facebookAlbum->album->facebookAccessToken);
$this->facebook->setFileUploadSupport(true);
$args = array();
$args["message"] = $picture->description;
$args["source"] = "#" . $picture->pictureContent;
$data = $this->facebook->api('/'. $facebookAlbum->id . '/photos', 'post', $args);
var_dump($data);
}
I keep getting :
curl_setopt_array(): The usage of the #filename API for file uploading is deprecated. Please use the CURLFile class instead in <b>acebook-php-sdk-master/src/base_facebook.php</b> on line <b>1005</b><br />
I think that the problem is that the image content is saved in the memory.
How can I use the variable in my memory in order to post it to Facebook ?
#filename has been deprecated in PHP >= 5.5.0 as stated here under the CURLOPT_POSTFIELDS description , So thats the reason why you got the error .
you have your answer here at this stack overflow thread, where different solutions are discussed . Also here is a snippet from RFC for the code.
Currently, cURL file uploading is done as:
curl_setopt($curl_handle, CURLOPT_POST, 1);
$args['file'] = '#/path/to/file';
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $args);
This API is both invonvenient and insecure, it is impossible to send
data starting with '#' to the POST, and any user data that is being
re-sent via cURL need to be sanitized so that the data value does not
start with #. In general, in-bound signalling usually vulnerable to
all sorts of injections and better not done in this way.
Instead of using the above method, the following should be used to
upload files with CURLOPT_POSTFIELDS:
curl_setopt($curl_handle, CURLOPT_POST, 1);
$args['file'] = new
CurlFile('filename.png', 'image/png'); curl_setopt($curl_handle,
CURLOPT_POSTFIELDS, $args);

PHP Upload Image from URL to Facebook [duplicate]

I'm trying to upload www hosted (e.g. http://www.google.se/intl/en_com/images/srpr/logo1w.png) files to a facebook album.
Creating an album works just fine, but I don't seem to uploading any photos. I'm using the facebook php-sdk ( http://github.com/facebook/php-sdk/ ) and the examples I already tried are:
Upload Photo To Album with Facebook's Graph API
How can I upload photos to album using Facebook Graph API
I'm guessing CURL uploads perhaps only can manage locally stored files and not web hosted ones.
Here's my code:
/*
Try 1:
$data = array();
$data['message'] = $attachment->post_title;
$data['file'] = $attachment->guid;
try {
$photo = $facebook->api('/' . $album['id'] . '/photos?access_token=' . $session['access_token'], 'post', $data);
} catch (FacebookApiException $e) {
error_log($e);
}
*/
// Try 2:
//upload photo
$file = $attachment->guid;
$args = array(
'message' => 'Photo from application',
);
$args[basename($file)] = '#' . realpath(file_get_contents($file));
$ch = curl_init();
$url = 'https://graph.facebook.com/' . $album['id'] . '/photos?access_token=' . $session['access_token'];
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
$data = curl_exec($ch);
//returns the photo id
print_r(json_decode($data,true));
...where attachment->guid contains the photo url.
I'm pretty much stuck right now...
i think the problem is here:
$args[basename($file)] = '#' . realpath(file_get_contents($file));
since you want to post a picture from another source (right?), you should save it temporarily on your own host.
i also needed to do something like this, and since i had to process the image, i used the following way:
$im = #imagecreatefrompng('http://www.google.se/intl/en_com/images/srpr/logo1w.png');
imagejpeg($im, 'imgs/temp/temp.jpg', 85);
$args['image'] = '#' . realpath('imgs/temp/temp.jpg');
the rest looks fine though
I'll suggest to use the Facebook php SDK, it will be easier and the code will work with future updates of the APIs:
Using the Graph API php sdk:
$fbk = new Facebook(/* conf */);
$fbk->setFileUploadSupport(true);
//If you are executing this in a script, and not in a web page with the user logged in:
$fbk->setAccessToken(/* access token from other sources */);
//To add to an album:
$fbk->api("/$albumId/photos", "POST",
array('source' => '#'. realpath($myPhoto), 'message' => "Nice photo"));
//To upload a photo directly (the album will be created automatically):
$fbk->api("/me/photos", "POST",
array('source' => '#'. realpath($myPhoto), 'message' => "Nice photo"));
Using cURL directly:
If your really want to use cURL, your code is almost correct, but the error is in the $args array:
$args = array(
'message' => 'Photo from application',
'source' => file_get_contents($file)
);
Since the key for the photo data is source, see the Facebook Doc
Note on the # in cURL:
Also notice that the # in cUrl means that the parameter will be replaced with the actual bytes of the file that follows the #, so it isn't required if you already put in the source parameter the actual bytes.
I'm guessing CURL uploads perhaps only can manage locally stored files and not web hosted ones.
No, that’s not the case.
But you need to give the full, publicly reachable HTTP URL to the image, without an # in front – and you have to use the parameter name url for this value.
https://developers.facebook.com/docs/reference/api/photo/:
You can also publish a photo by providing a url param with the photo's URL.

Offline upload photo to Facebook with photo from web server

I've searched around everywhere without luck and found lots of examples to upload a photo to facebook when the user is in a session (i.e. the user is physically sitting at the computer and accessing the web page). I've tried the samples, and they work.
I noticed this unanswered question from last year on the same issue
Stackoverflow Question
My current app lets the user authorise off-line updates and I store the access_token, user_id, etc. and I can succesfully post to a users wall when they are offline.
I'm really struggling getting something to work with posting a photo to the users wall. Reading the Facebook documentation, I'm thinking you can only upload photos using multipart/form-data!?!?
That wouldn't work if the user isn't at their computer. Can you upload photos that are stored on a directory on my server?
Here's my code so far. Remember, this doesn't use a facebook session as the access_code has already been granted and stored beforehand. As I mentioned, posting to a users wall already works with this approach.
$filename= "#/myphotodir/filename.jpg");
$url = "https://graph.facebook.com/".$uid."/photos"; //$uid is fb user id
$ch = curl_init($url);
$attachment = array('access_token' => $access_token,
'app_id' => $app_id,
'name' => "A photo from me...",
'fileUpload' => true,
'message' => "my message",
'image' => $filename,
);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result= curl_exec($ch);
curl_close ($ch);
Edit: $result comes back false... forgot to add that.
Any help would be appreciated.
Many thanks,
Dean
You should use the Facebook SDK. With the Facebook SDK, you can use:
$facebook->setFileUploadSupport( true );
$parameters = array(
'access_token' => 'ACCESS_TOKEN_HERE',
'message' => 'PHOTO_CAPTION',
'image' => '#' . realpath( '/path_to_file.jpg' ) // Notice the # sign
);
$facebook->api( '/user_id/photos', 'post', $parameters );
This will post the photo to their default album. If you replace user_id with an album_id you can post to a specific album.

Categories