0

I use Google's My maps. The problem is that Google makes many restrictions in using their API, which makes it hard to insert or retrieve data from the map automatically.

I have a direct download link to this map file which downloads the KML file to any device when clicked.

What I want is a code in PHP to open this link, download the KML file and save it automatically to my server.

i have seen this in a different question but it didnt work for me:

shell_exec("wget -P /target/directory/ http://download.link.com/download.zip");

I think the problem is that my link does not link to a file (does not end in .zip or other file extensions) This is an example for my link: here

Thank you!

Ziv Oriol
  • 5
  • 1
  • 6
  • Depeneding on googles restrictions, you could easily do this with curl (which avoids using exec at all costs), or even file_get_contents (if you have fopen wrappers enabled). – IncredibleHat Sep 18 '18 at 13:30

2 Answers2

1

This is one solution to save the information into a file using 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);

Make sure you give permission to the folder on your server where the file will be created.

0

You can use file_put_contents and file_get_contents functions, like so:

$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));
HTMHell
  • 5,761
  • 5
  • 37
  • 79