3

我在下面有这个图片链接:

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

但是如果你点击它并在浏览器中查看它,图像文件的实际 url 是这样的:

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

知道如何解析上面的图像链接以使用 php 获取实际的 jpg 文件吗?

4

3 回答 3

5
<?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

于 2012-11-23T14:07:13.113 回答
0

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.

于 2012-11-23T14:07:46.157 回答
0

你也可以这样做:

header('Content-type:image/png');
$file=file_get_contents($url);
于 2012-11-23T14:12:57.167 回答