Facebook API uploads photo from url

I have a website with a photo gallery, and I would like to upload each photo (one by one) to my facebook page (and not the wall). I managed to post a message, but now I want to upload a photo to the wall of the FB page by downloading an existing image from the server URL (I do not want to upload locally again). Is it possible?

+6
source share
4 answers

Yes you can do it
Example In Graph Api Explorer
Make a message for the call, set the url to https://graph.facebook.com/me/photos ,
Add a field with the message key and the value "any custom message"
Add another field with url key and value https://appharbor.com/assets/images/stackoverflow-logo.png
click send

+12
source

You need to know the album id and make a POST request:

 https://graph.facebook.com/albumid/photos?access_token=$access_token 

You will find the album identifier included in the album and look at the URL. There will be something like https://www.facebook.com/media/set/?set=a.XXXXXXXXXXXX.YYYY.ZZZZZZZZZZ&type=3

Your album ID is XXXX.

+1
source

this is what i use:

 $facebook = new Facebook(array( 'appId' => FACEBOOK_APP_ID, 'secret' => FACEBOOK_SECRET, 'cookie' => true, 'fileUpload' => true, )); $user = $facebook->getUser(); // Get the UID of the connected user, or 0 if the Facebook user is not connected. $facebook->setFileUploadSupport(true); if($user == 0) { // If the user is not connected to your application, redirect the user to authentication page /** * Get a Login URL for use with redirects. By default, full page redirect is * assumed. If you are using the generated URL with a window.open() call in * JavaScript, you can pass in display=popup as part of the $params. * * The parameters: * - redirect_uri: the url to go to after a successful login * - scope: comma separated list of requested extended perms */ $login_url = $facebook->getLoginUrl($params = array('redirect_uri' => REDIRECT_URI,'scope' => PERMISSIONS_REQUIRED)); echo ("<script> top.location.href='".$login_url."'</script>"); } else { // if the user is already connected, then fetch access_token and user information or show some content to logged in user. try { $access_token = $facebook->getAccessToken(); // Gives you current user access_token $user = $facebook->api('/me'); // Gets User information based on permissions the user has granted to your application. } catch(FacebookApiException $e){ $results = $e->getResult(); // Print results if you want to debug. } } $img = './upload/'.$image_path; $args = array( 'message' => 'Some Message', 'access_token'=>urlencode($access_token), ); $args[basename($img)] = '@'.realpath($img); $ch = curl_init(); $url = 'https://graph.facebook.com/me/photos'; curl_setopt($ch, CURLOPT_URL, $url); curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0); 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); $response = json_decode($data,true); 
+1
source
  $config = array('appId' => $config['App_ID'],'secret' => $config['App_Secret']); $facebook = new Facebook($config); // sets our access token as the access token when we call // something using the SDK, which we are going to do now. $facebook->setAccessToken($access_token); $page_id = "XXXXXXXXXXXXXXX"; $page_access_token = ""; $result = $facebook->api("/me/accounts"); foreach($result["data"] as $page) { if($page["id"] == $page_id) { $page_access_token = $page["access_token"]; break; } } $facebook->setFileUploadSupport(true); $photo = "http://cdn.sstatic.net/stackexchange/img/logos/so/so-logo.png"; $args = array( 'access_token' => $page_access_token, 'message' => "message here", 'url' => $photo, ); $post = $facebook->api("/$page_id/photos","post",$args); 
0
source

Source: https://habr.com/ru/post/919593/


All Articles