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 !

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.";
}
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.