PHP无法保存外部图像到文件


php cannot save external image to file

由于某些原因,我无法将谷歌地图图像保存到文件中。

下面是我的代码:
 $rs=mysqli_fetch_assoc(mysqli_query($dbh,"select latitude,longitude from locations where locid=$locid"));
 $lat=$rs[latitude];$lon=$rs[longitude];
 $mapUrl="https://maps.googleapis.com/maps/api/staticmap?center=$lat,$lon&zoom=13&size=400x280&key=".google_apikey;
 $tmp="/srv/www/data/maps/tmp.png";
 touch($tmp);chmod($tmp,0777);
 file_put_contents($tmp,file_get_contents($mapUrl));

结果图像保存在tmp.png is 0 bytes 然而,当我去到地图url显式显示

我不太确定这里有什么问题

进入本页选择的答案:从URL复制图像,更改名称并保存到文件夹

根据你的需要调整它,因为它工作得很好。我也有类似的问题,一些堆栈解决了我的麻烦,确保你的dirs是777,你是SSL兼容的。我的问题原来是午夜时分对细节的关注。我很习惯在这里根"/images/my/path/"。扩展',我忘了把它改成'images/…'。哎!有时候就是这么简单。