954,580 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Save Image from a URL which redirects to another URL

Hello Guyz,

I wish that the Title of the thread explains itself a lot.

I would like to Save an image from an HTTP URL which actually redirects to another URL.

Actually, I am trying to Save the Profile picture of Facebook.

Example : https://graph.facebook.com/thakurbhai/picture

Now when you will open the link, it will redirect to another URL/jpeg image, which I would like to save.

I've tested my Code with normal image url (.jpg) and it works fine for that. But for redirecting URL, it saves a file example.jpg with size 0 bytes.

Help please !

AnkurThakur
Newbie Poster
10 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

Nevermind ! I resolved it !

What I did :

1. Get the Headers of the URL.
2. Filter the Headers and look for Location header.
3. Now Use it ;)

<?php
$url = 'https://graph.facebook.com/thakurbhai/picture';

$location = get_headers($url, 1);

if (array_key_exists('Location', $location))
{
	echo $location['Location'];
}
?>


Remember: I am Getting Headers for a Secure URl, i.e, HTTPS (https://graph.facebook.com/thakurbhai/picture)
For doing that, you must have php_openssl enabled on your server.

Or you can do something like this :

<?php
if (extension_loaded('openssl'))
{

$url = 'https://graph.facebook.com/thakurbhai/picture';

$location = get_headers($url, 1);

if (array_key_exists('Location', $location))
{
	echo $location['Location'];
}

}
else
{
	echo "Sorry ! Open SSL Not enabled.";
}
?>
AnkurThakur
Newbie Poster
10 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: