I have simple php script, for get image from Facebook and download user profile image to my server.
$url = 'https://graph.facebook.com/'.$idUser.'/picture?width=200&height=200&redirect=false';
$data = json_decode(file_get_contents($url), true);
$url = $data["data"]["url"];
$path = $idUser.'.jpg';
file_put_contents ($path, file_get_contents($url));
In my CMS site I try to open user profile page, where all time I execute that script, because I want to see actual user profile image. But when user change user profile image my script is do wrong. file_get_contents download old image, when I refresh page or new image randomly. What that mean? Where is error?
That still my answer file_get_contents no caching?
but not work for me(
> UPDATE:
Facebook wrong get me json link randomly
I ran your code locally and got the same problem. Something is getting cached along the line, so I added a cache buster to end of the URL, and it forces the new image.
$url = 'https://graph.facebook.com/'.$idUser.'/picture?width=200&height=200&redirect=false&' . time();
Related
This might seem a repeated question, but trust me I have read many topics here, none of the solutions worked for me 😑
What I particularly aim to do is, let's say I have this image URL - https://catfriendly.com/wp-content/uploads/2019/06/AdobeStock_53119595.jpeg, and I want to Download it on my user's phone. I would redirect the user to URL when he/she clicks the download button, and then the URL's image would be downloaded on the user's phone. Is this possible to do?
I've tried this code,
<?php>
$url = 'https://catfriendly.com/wp-content/uploads/2019/06/AdobeStock_53119595.jpeg';
$img = '/Downloads/one.jpeg';
file_put_contents($img, file_get_contents($url));
?>
What's wrong in this one? Thanks!
I think the problem with your code is that it is executed on your server, but not on the user's phone.
So you have to modify your code a little:
<?php
$url = 'https://catfriendly.com/wp-content/uploads/2019/06/AdobeStock_53119595.jpeg';
// leave this out because it only stores the image on the server
// $img = '/Downloads/one.jpeg';
// file_put_contents($img, file_get_contents($url));
// get the image content:
$img_content = file_get_contents($url);
// tell the client that you will send an image
header('Content-Type: image/jpeg');
// tell the client not to display the image, but to download it
header('Content-Disposition: attachment; filename="YourFilenameHere.jpg"');
// finally, output the image
echo $img_content;
?>
I am trying to develop a login to my website functionality with Facebook account using Facebook Graph API.
I am in trouble to retrieve profile image of logged in user because Facebook returns URL but when I try it on the navigator it says that it needs permission to download it.
For example the URL:
https://platform-lookaside.fbsbx.com/platform/profilepic/?asid......
PS : I make the 3 points
I want to know if there is any method to get a correct URL to save it in the database or if there is any method to download the image with PHP, I tried this on my end, but no success.
$img = file_get_contents($dataata['picture']['url']);
$file = '/img/test.jpg';
file_put_contents($file, $img);
Message returned is :
file_put_contents(/img/test.jpg): failed to open stream: No such file
or directory
Any solution?
I am facing one problem i want to save data after downloading an image. My image has been successfully downloaded after that i want to maintain the history of downloads.Here is my Function:-
enter code here
public function UserImageDownlaod($userid=null,$imageid=null){
$imagedata = DB::table('alt_images')->where('id',$imageid)->first();
$imagedata = json_decode(json_encode($imagedata),true);
$destination = 'images/ContributorImages'.'/';
$pathToFile = $destination.$imagedata['img'];
return response()->download($pathToFile);
//Now i want to save the history of downloaded images
$history= new DownloadHistory;
$history->user_id = Auth::user()->id;
$history->alt_image_id = $imageid;
$history->save();
}
Now you can see after this return response()->download($pathToFile) line i want to save the history data. I want when User click on OK button then data will saved on history table. Can anyone help me.
The popup you showed us is from the browser. At the point, you see this popup, the browser might already started to download the file in the background. As this is part of your browser and you dont have any callback about pressing ok/cancel, there is no direct way to determine, what the user did.
A simple workaround would be, to show the user an alert box in javascript and ask, if he really wants to download the file.
Download
I am using the PHP quick start project example to display the timeline's attachment (image):
<?php
if ($timeline_item->getAttachments() != null) {
$attachments = $timeline_item->getAttachments();
foreach ($attachments as $attachment) { ?>
<img src="<?php echo $base_url .
'/attachment-proxy.php?timeline_item_id=' .
$timeline_item->getId() . '&attachment_id=' .
$attachment->getId() ?>" />
<?php
}
}
?>
Now I need to save the image to the server so I can resize it and use it elsewhere.
I have tried a few variations of file_put_contents, fopen, and curl but it seems attachment-proxy.php is not returning the image in a format that any of these expect.
How can save a Timeline Attachment to my server?
SOLUTION: Based on Prisoner's response I took another look at the attachment-proxy.php file. It is returning the image as a string. I had unsuccessfully tried file_put_contents($img, file_get_contents("attachment-proxy.php....")); before.
Turns out I don't need the file_get_contents() part.
I altered the last few lines of attachment-proxy.php to this:
$img = $_GET['timeline_item_id'].'.jpg';
$image = download_attachment($_GET['timeline_item_id'], $attachment);
file_put_contents($img, $image);
It works. It saves the image to my server with the ID as the file name.
Thanks.
Have you checked to see what it is returning? The attachment_proxy.php requires OAuth to have been completed, and will redirect you through the OAuth flow if this hasn't been done. So it may very well be that it is saving the HTML for the OAuth login page, or the information from the redirect page.
However, if you're trying to setup something on your server that calls your own server's attachment_proxy.php page... you're jumping through additional unnecessary hoops.
You can probably take a look directly at attachment_proxy.php to see how it is getting the attachment data from Google's servers, and then use this same method to get them and store them on your server instead of just feeding it out for the img tag. Looking at https://github.com/googleglass/mirror-quickstart-php/blob/master/attachment-proxy.php it seems like most of the work is done in a call to download_attachments() which is located in https://github.com/googleglass/mirror-quickstart-php/blob/master/mirror-client.php. You should be able to either borrow the code from download_attachments() or call it directly yourself.
uploading image with file_put_contents make 0 byte file.
here is the code that I use. I extract facebook image url and put it into web server.
$fb_image_url = 'https://example.com/229282.jpg'
$filename = substr($fb_image_url, strrpos($fb_image_url, '/') + 1);
file_put_contents('upload/user_pic/original/'.$filename, file_get_contents($fb_image_url));
after I do this, the server receive file name successfully, but it is 0 bytes.
I checked php.ini, and allow_url_fopen is ON.
uploading folder permission is also fine.
To copy images from facebook, script/php program require permission for same. if program/ FB API dont passes validation/permission check, FB dont allows to download any image.
Its looks like your Application don't have permission to download/copy this image from Facebook that's why your getting 0 bytes.
Try giving PUBLIC access to image and keep FB account logged in while coping image
I just put that URL into the browser:
https://fbcdn-profile-a.akamaihd.net/hprofile-ak-snc4/274661_1171545457_6475606_n.jpg
And received 404 Not Found in response. This would explain why you get empty file locally. I strongly suggest that you load the data first, verify what you received and then, if validation passes, save it locally.
here is the solution.
I had a problem with facebook profile address.
$fb_image_url = 'https://graph.facebook.com/'.$facebook_id.'/picture?type=large';
$filename = $fb_id.'_'.time().'.jpg';
$result = file_get_contents($fb_image_url);
file_put_contents('upload/user_pic/original/'.$filename, $result);
with this_ it worked fine. Yeah! Thank you everyone!