Hello,
I am trying to upload photo to facebook through my facebook app.
Here is my code:
$photo_details = array(
'message'=> 'my photo'
);
$file = "http://upload.wikimedia.org/wikipedia/commons/thumb/8/81/Waves_lajolla.jpg/300px-Waves_lajolla.jpg";
$photo_details['image'] = '#'.realpath($file);
try {
$upload_photo = $facebook->api('/'.'almbumId'.'/photos', 'post', $photo_details);
} catch (FacebookApiException $e) {
echo 'Error'.$e;
error_log($e);
}
but it always gives me
CurlException: 26: failed creating formpost data
Your image path can't be an URL, it has to be local. For example:
$file='images/logo.jpg'; //Example image file
But did you get any solution on how to post image from an URL by any chance? If so please share as I'm looking to find a solution to post the image from an URL too.
Related
My issue is already discussed here
couldn't open file "" error in Facebook PHP API
and my php script is
//variables we are going to post to facebook
$fbPermissions = 'publish_stream,user_photos'; //Required facebook permissions
$PicLocation = 'fb_cover_images/cover7.jpg';
$msg_body = array(
'message' => 'I liked this pic from '. $homeurl .
' it is perfect for my cover photo.',
'url' => "http://my_site.com/$PicLocation"
);
if ($fbuser){ //user is logged in to facebook, post our image
try {
$uploadPhoto = $facebook->api('/me/photos', 'post', $msg_body );
}
catch (FacebookApiException $e) {
echo $e->getMessage(); //output any error
}
}
else{
$loginUrl = $facebook->getLoginUrl(
array('scope'=>$fbPermissions,'return_url'=>$return_url));
header('Location: ' . $loginUrl);
}
If I set 'url' => "http://my_site.com/$PicLocation" then getting
(#200) Permissions error
and If I set 'url' => "#http://my_site.com/$PicLocation" then getting
couldn't open file "http://my_site.com/fb_cover_images/cover7.jpg"
message though if I hit "http://my_site.com/fb_cover_images/cover7.jpg", I can view image and permission to folder+image is set to 777.
Please guide what I am doing wrong....
I have searched but fail.
I also tried [PHP + Facebook: how to upload photo on the wall? but same rubbish message (#200) Permissions error
Also Upload Photo To Album with Facebook's Graph API
I don't know what the hell is going on with FB API.
Try adding the following line to the top of your code, after setting up Facebook:
$facebook->setFileUploadSupport( true );
Then, change your $msg_body to the following:
$msg_body = array(
'name' => 'I liked this pic from '. $homeurl . ' it is perfect for my cover photo.',
'source' => "http://my_site.com/$PicLocation"
);
The name is the caption for the image, and the source is the URL to the file. You only need to prefix # to the source if you are referencing a local file.
Okay so facebook want me to send an image stored locally on the server but i have the images stored in a BLOB in the database...
try {
$file = 'http://blaze-craft.com/matt/get.php?id=' . $lastid;
$post_data = array(
"message" => "Uploaded using the Funnymemes app!",
"source" => $file
);
$data['photo'] = $facebook->api("/me/photos", 'post', $post_data);
}
catch (FacebookApiException $e) {
}
but its not uploading... I got this code from the facebook api doc's so im not sure whats going on?
any ideas?
In order to photo upload to work you have to fix these:
Image files should be stored in filesystem (i.e. save BLOB back to
the disk)
Use # before image path in $post_data array.
For this purpose i would do something like:
try
{
$url = 'http://blaze-craft.com/matt/get.php?id=' . $lastid;
$saveas = '/images/image.jpg';
$res = #file_put_contents($saveas, file_get_contents($url));
if($res === false) throw new Exception('Cannot fetch image');
$post_data = array(
"message" => "Uploaded using the Funnymemes app!",
"source" => '#' . $saveas;
);
$data['photo'] = $facebook->api("/me/photos", 'post', $post_data);
}
catch (FacebookApiException $e) { }
catch (Exception $e) { }
This is a rather strange issue..
I am not able to publish a facebook video link on my wall.
The following code works perfectly fine...
$attachment = array(
'link' => "www.google.com"
);
$publish = $facebook->api('/me/feed', 'post', $attachment);
But the following code doesnt
$attachment = array(
'link' => "http://www.facebook.com/video/video.php?v=10150238694155484"
);
$publish = $facebook->api('/me/feed', 'post', $attachment);
It doesn't post anything to my wall.
Has anyone seen the same issue?
As far as I remember, facebook made the picture parameter required for video links
Add it into try catch
try{
$attachment = array(
'link' => "http://www.facebook.com/video/video.php?v=10150238694155484"
);
$publish = $facebook->api('/me/feed', 'post', $attachment);
}
catch (Exception $e){
echo "<pre>";
print_r($e);
echo "</pre>";
}
it will show you all error details needed to resolve your problem
Using the facebook php-sdk, users can upload photos to facebook from our site. I can create the album and upload the photos. But can't get the URL to the newly created Album :(
Here's what I do ...
// create album
$albumDetails = array(
'name' => 'Fun images'
);
$album = $facebook->api('/me/albums', 'post', $albumDetails);
$albumID = $album['id'];
// upload photos
foreach ($images as $image) {
$file = FACEBOOK_IMAGES_DIR . $image->image_id . '.jpg';
$photoDetails = array(
'message'=> 'by choreboy'
);
$photoDetails['image'] = '#' . realpath($file);
$photoData = $facebook->api('/'.$albumID.'/photos', 'post', $photoDetails);
}
// Graph API says I can get link to the album:
// http://developers.facebook.com/docs/reference/api/album
// I thought I could get to the link data this way. But returns an empty array
$data=$facebook->api("/{$albumID}/photos?access_token={$session['access_token']}");
var_dump($data);
Maybe you are missing the first line in this code.
This URL redirects to your album:
$album_url = 'http://www.facebook.com/' . $albumID;
Here is the code:
$file= 'bbbb.jpg';
$data = array(
basename($file) => "#".realpath($file),
"caption" => "Uploaded using graph api",
"aid" => '13595',
"access_token" => $accessToken,
'method' => 'photos.upload'
);
$sds =$facebook->api($data);
This is the error
Uncaught CurlException: 26: failed creating formpost data
What to do?
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 for the current user.
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.
$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.
$args = array('message' => 'Photo Caption');
$args['image'] = '#' . realpath($FILE_PATH);
$data = $facebook->api('/'. $ALBUM_ID . '/photos', 'post', $args);
print_r($data);
3 - Target Album with Access Token
This example will upload a photo to a specific album which requires an access token.
$args = array('message' => 'Photo Caption');
$args['image'] = '#' . realpath($FILE_PATH);
$data = $facebook->api('/'. $ALBUM_ID . '/photos?access_token='. $ACCESS_TOKEN, 'post', $args);
print_r($data);
your $data array should have "message" instead of "caption",
also, remove "aid", "method", and "access_token"
your $data has to have the file data and "message", that is it.
$sds =$facebook->api('/me/13595/photos', 'POST', $data);
where instead of 13595 just use the variable with the album aid
also, if needed, access_token is best appended to api uri like this:
$sds =$facebook->api('/me/13595/photos?access_token='.$access_token, 'POST', $data);
also, if the php sdk doesn't work for you, I have successfully used cURL instead if your php installation supports it. in that case see cURL example at Upload Photo To Album with Facebook's Graph API
The latest version of the Facebook PHP SDK wont work with the above examples without the following update to the code.
class Facebook {
...
*Line #539*
protected function makeRequest($url, $params, $ch=null) {
if (!$ch) {
$ch = curl_init();
}
if( isset($params['doMultiPart']) ) {
$doMultiPart= true;
unset($params['doMultiPart']);
} else {
$doMultiPart= false;
}
$opts = self::$CURL_OPTS;
$opts[CURLOPT_POSTFIELDS] = $doMultiPart ? $params : http_build_query($params, null, '&');
...
Basically the problem is that the PHP SDK uses "curl_setopt_array" which if you pass it a url encoded string as the option value it will pass the data as application/x-www-form-urlencoded when what you really want is multipart/form-data; to do this we simply switch to passing in the array of options if we have a param of doMultiPart in the params array.
This was a quick hack I put together to get something working, probably need to review the code to make sure it doesnt break anything else you are doing. Otherwise enjoy.