PHP从PHP脚本生成的url下载图像

PHP从PHP脚本生成的url下载图像,php,Php,我有问题。我需要从URL下载图像,但图像URL不是以JPG、PNG或其他img文件类型结尾。此图像由PHP脚本生成 这是一张图片: 如何使用PHP脚本下载此图像?如果PHP.ini中有allow\u url\u fopen=1,则可以使用 ,否则,如果安装了php curl,则可以使用 。如果您既没有“允许url”也没有“卷曲”,则可以使用 如果您检查该URL的标题,您将看到内容类型:image/png。您需要解释content-type头,并为文件的任何类型使用适当的扩展名进行保存。 $im

我有问题。我需要从URL下载图像,但图像URL不是以JPG、PNG或其他img文件类型结尾。此图像由PHP脚本生成

这是一张图片:


如何使用PHP脚本下载此图像?

如果PHP.ini中有
allow\u url\u fopen=1
,则可以使用

,否则,如果安装了php curl,则可以使用

。如果您既没有“允许url”也没有“卷曲”,则可以使用


如果您检查该URL的标题,您将看到
内容类型:image/png
。您需要解释content-type头,并为文件的任何类型使用适当的扩展名进行保存。
$image_binary=file_get_contents($url);
$ch=curl_init($url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$image_binary=curl_exec($ch);
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
    die("socket_create() failed: reason: " . socket_strerror(socket_last_error()));
}
$result = socket_connect($socket, $address, 80);
if ($result === false) {
    die("socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)));
}
$out=implode("\r\n",array("GET ".substr($url,strpos($url,"/"))." http/1.0","User-Agent: foobar","","");
socket_send($socket,$out,strlen($out),0);
$last='';
$total='';
while(!!socket_recv($socket,$last,2048,MSG_WAITALL)){
    $total.=$last;
    $last='';
}
socket_close($socket);
$image_binary=substr($total,strpos($total,"\r\n\r\n"));