从直接下载链接下载文件并保存到服务器-PHP
我使用谷歌的“我的地图”。问题是谷歌在使用API时有很多限制,这使得它很难从地图中自动插入或检索数据 我有一个指向此地图文件的直接下载链接,单击此链接可将KML文件下载到任何设备 我想要的是一个PHP代码来打开这个链接,下载KML文件并将其自动保存到我的服务器 我在另一个问题中看到了这一点,但它对我不起作用:从直接下载链接下载文件并保存到服务器-PHP,php,server,download,mymaps,Php,Server,Download,Mymaps,我使用谷歌的“我的地图”。问题是谷歌在使用API时有很多限制,这使得它很难从地图中自动插入或检索数据 我有一个指向此地图文件的直接下载链接,单击此链接可将KML文件下载到任何设备 我想要的是一个PHP代码来打开这个链接,下载KML文件并将其自动保存到我的服务器 我在另一个问题中看到了这一点,但它对我不起作用: shell_exec("wget -P /target/directory/ http://download.link.com/download.zip"); 我认为问题在于我的链接没有
shell_exec("wget -P /target/directory/ http://download.link.com/download.zip");
我认为问题在于我的链接没有链接到文件(没有以.zip或其他文件扩展名结尾)
以下是我的链接示例:
谢谢大家! 您可以使用和函数,如下所示:
$path = "/target/directory/file.kml"; // Where the file should be saved to
$link = "https://www.google.com/maps/d/u/0/kml..."; // Download link
file_put_contents($path, file_get_contents($link));
这是一种使用cURL将信息保存到文件中的解决方案
<?php
$ch = curl_init();
// You can add here the URL contains the information which should be fetched
curl_setopt($ch, CURLOPT_URL,'https://www.google.com/maps/d/kml?mid=1FcgdJK9tLMt_dygTbxObJHjyCoo&forcekml=1&cid=mp&cv=qDJ98FtrON4.he.');
// You can define the name which will be created with the extension, in this case it's .KML
$openFile = fopen('file.KML', 'w+');
// Here Curl will save the fetched information into the file.
curl_setopt($ch, CURLOPT_FILE, $openFile);
curl_exec ($ch);
curl_close ($ch);
fclose($openFile);
依靠谷歌的限制,你可以通过curl(不惜一切代价避免使用exec)或者甚至是file_get_内容(如果你启用了fopen wrappers)轻松实现这一点。这正是我所需要的:)