anagnam
anagnam

Reputation: 457

getting the correct image url

i have this image link below:

http://ws.assoc-amazon.com/widgets/q?_encoding=UTF8&ASIN=B008EYEYBA&Format=_SL110_&ID=AsinImage&MarketPlace=US&ServiceVersion=20070822&WS=1&tag=mytwitterpage-20 

but if you click it and view it in a browser, the actual url of the image file is this:

http://ecx.images-amazon.com/images/I/418lsVTc0aL._SL110_.jpg

any idea how I can parse the image link above to get the actual jpg file using php?

Upvotes: 3

Views: 123

Answers (3)

cezar
cezar

Reputation: 94

You could also do something like this :

header('Content-type:image/png');
$file=file_get_contents($url);

Upvotes: 0

Tchoupi
Tchoupi

Reputation: 14691

Use get_headers(), and get the Location: header:

$headers = get_headers($url);
echo $headers['Location'];

Note:

This is the most basic version and it will work as long as there is only 1 redirect. If you run into more complex issues, use @aykut's solution.

Upvotes: 0

aykut
aykut

Reputation: 3094

<?php

function get_url($url) {
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    curl_exec($ch);

    if (!curl_errno($ch)) {
        $url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
    }

    curl_close($ch);

    return $url;
}

echo get_url("http://ws.assoc-amazon.com/widgets/q?_encoding=UTF8&ASIN=B008EYEYBA&Format=_SL110_&ID=AsinImage&MarketPlace=US&ServiceVersion=20070822&WS=1&tag=mytwitterpage-20");

Source

Upvotes: 5

Related Questions