我如何从一个HTTP url拉图像和保存到https服务器,然后加载


How would I pull an image from an HTTP url and save to HTPS server and then load?

我想弄清楚如何从第三方网站上拉出一个经常更改的图像。基本上,我使用Vbulletin软件,并希望避免混合内容警告-托管图像(HTTP)从另一个网站到我的(HTTPS)。我想函数基本上在php中调用图像,保存图像到我的服务器上的文件夹,然后一个php函数调用保存的图像在我的服务器上显示它。想法吗?谢谢。我一直在file_get_contents上打开流失败…

    ob_start();

//Get the file
$content = file_get_contents("http://www.defconwarningsystem.com/current/defcon.jpg");
//Store in the filesystem.
$fp = fopen("/images/defcon/defcon.jpg", "w");
fwrite($fp, $content);
fclose($fp);

$html = ob_get_clean();
return $html;

变化

$content = file_get_contents("http://www.defconwarningsystem.com/current/defcon.jpg");

$content = file_get_contents("//defconwarningsystem.com/current/defcon.jpg");

看看是否可以

如果file_get_contents不起作用,请尝试cURL。参见php参考

的示例用法