https连接中加载的Http内容


Http content loaded in https connection

我的Webapp使用https连接/ssl证书运行。我需要向用户显示图片。我通过API请求获得图片的链接,然后将它们链接起来。遗憾的是,图片地址是http,所以浏览器显示网站上有不安全的部分,这一定不是…

我可以下载图片,然后链接到合适的图片,但我认为这可能有点耗时,不是处理这个问题的最佳方式。

有人知道更好的解决方案吗?我使用php、jquery和javascript。

您必须在服务器上编写一个代理并通过它显示所有图像。基本上,您的URL应该是:

$url = 'http://ecx.images-amazon.com/images/I/51MU5VilKpL._SL75_.jpg';
$url = urlencode($url);
echo '<img src="/proxy.php?from=' . $url . '">';

和proxy.php:

$cache = '/path/to/cache';
$url = $_GET['from'];
$hash = md5($url);
$file = $cache . DIRECTORY_SEPARATOR . $hash;
if (!file_exists($file)) {
    $data = file_get_contents($url);
    file_put_contents($file, $data);
}
header('Content-Type: image/jpeg');
readfile($file);

好的,我有一个流媒体示例。当然,你需要根据自己的需要进行调整。

假设您在服务器上创建了一个名为mws.php的php文件,其中包含以下内容:

if (isset($_GET['image']))
{
   header('Content-type: image/jpeg');
   header('Content-transfer-encoding: binary');
   echo file_get_contents($_GET['image']);
}

在网络上查找任何图像,例如:

http://freebigpictures.com/wp-content/uploads/2009/09/mountain-stream.jpg

现在,您可以显示该图像,就好像它位于您自己的安全服务器上一样,网址为:

https://<your server>/mws.php?image=http://freebigpictures.com/wp-content/uploads/2009/09/mountain-stream.jpg

当然,如果您多次需要图像,最好将其存储在本地,并且您必须包含正确的代码才能从Amazon MWS ListMatchingProducts获得图像,但这是基本思想。

请不要忘记保护您的脚本不被滥用。