Facebook - How to upload a photo via URL?

For example, I have WordPress installed on my website. I want to upload images to Facebook via URL. MY image Url:

http://example.com/wp-content/abc123.jpg

Now I want to upload this image to Facebook through my own website directly to a specific album. I am completely N00b! So I don’t know anything about coding.

My Goal is to create a cover website.

* SECOND UPDATE *: Here is the code I'm using right now:

<?php $app_id = "12354"; $app_secret = "1213243434"; $post_login_url = "http://example.com/sdsds"; $album_id = "1234224"; $photo_url = "http://example.coom/test.jpg"; $photo_caption = "my caption"; $code = $_REQUEST["code"]; //Obtain the access_token with publish_stream permission if (!$code){ $dialog_url= "http://www.facebook.com/dialog/oauth?" . "client_id=" . $app_id . "&redirect_uri=" . urlencode( $post_login_url) . "&scope=publish_stream"; echo("<script>top.location.href='" . $dialog_url . "'</script>"); } else { $token_url="https://graph.facebook.com/oauth/access_token?" . "client_id=" . $app_id . "&client_secret=" . $app_secret . "&redirect_uri=" . urlencode( $post_login_url) . "&code=" . $code; $response = file_get_contents($token_url); $params = null; parse_str($response, $params); $access_token = $params['access_token']; // POST to Graph API endpoint to upload photos $graph_url= "https://graph.facebook.com/" . $album_id . "/photos?" . "url=" . urlencode($photo_url) . "&message=" . urlencode($photo_caption) . "&method=POST" . "&access_token=" .$access_token; echo '<html><body>'; echo file_get_contents($graph_url); echo '</body></html>'; } ?> 

So, from this code I can upload "test.jpg" to facebook, but now the problem is that I do not want to upload only one image, I want to upload many images, so I do not want to change "$ photo_url =" http: / /example.coom/test.jpg ";" this code for new photos any help would be appreciated!

+4
source share
1 answer

You can’t download the photo from the URL, it’s not even considered a “download”, since you don’t actually upload the pic, just enter the URL.

What you need to do is get the actual image data and send it directly to facebook via a POST request to this URL:

 https://graph.facebook.com/ALBUM_ID/photos 

with parameters: message and source.

If you do not have the image locally on the server, you need to upload it, read its “content” and then send it to facebook.


Edit

Thanks to the comment by @TommyBs, I see that it is available for download via url. I will leave the answer as it talks about the alternative.


2nd Edit

If you want to upload multiple images, you have 2 options (as I see):

(1) Use the Batch Requests provided by facebook. You will have to check if it works with this particular method, I have no idea, since I have never tried uploading the image to facebook using the URL.

(2) Send an image request, something like:

 function uploadPicture($albumId, $photoUrl, $message, $token) { $url= "https://graph.facebook.com/" . $albumId . "/photos?" . "url=" . urlencode($photoUrl) . "&message=" . urlencode($message) . "&method=POST" . "&access_token=" . $token; return file_get_contents($url); } 

Then just reuse this function with various parameters.

+4
source

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


All Articles