简体   繁体   English

将 google 位置 api 照片保存到我的服务器

[英]Saving google places api photos to my server

i need to save the images returned from the google places API to my server.我需要将从谷歌地点 API 返回的图像保存到我的服务器。 I've tried :我试过:

$url= 'https://maps.googleapis.com/maps/api/place/photo?key=API_KEY_HERE&photo_reference=CoQBcwAAAAtXfnhO3WCagOlY4p4Px7N8Pcks_N-cLbMQzruT-AdWRZoyJgABVUUcuZ_4bbcUdReloBl2zGd80W4E4w-N_kyab4xz4S3ZZIRVECCJP1u7JsXOfxEsx4XQbL-HeMBRKzNul0XSdy-Dv4495i_8-SqYqTBZMaLvn1YLaVM3aAzOEhBgLV4lpKeM39L6gb9wBbU6GhSmCkYp8djpm9_iaqkS93z4ekLnNg&maxheight=200'

$target = md5($array['result']['photos'][0]['photo_reference']);
$newname = 'img/googlephotos/newimage'.$target;
file_put_contents($newname,file_get_contents($url));

But it's not working - when it's saving it saves an empty file and not the image - I think it is missing the file extension but I'm not sure how to get it.但它不起作用 - 保存时它会保存一个空文件而不是图像 - 我认为它缺少文件扩展名,但我不确定如何获取它。 does anyone know of any ways to save google places photo?有谁知道保存谷歌地点照片的任何方法?

I'm using PHP to write my code.我正在使用 PHP 编写代码。

Any help would be great任何帮助都会很棒

You should be aware of the restrictions in Terms of Service.您应该了解服务条款中的限制。 Particularly, paragraph 10.5 (d) says:特别是,第 10.5 (d) 段说:

You will not pre-fetch, cache, index, or store any Content to be used outside the Service, except that you may store limited amounts of Content solely for the purpose of improving the performance of your Maps API Implementation due to network latency (and not for the purpose of preventing Google from accurately tracking usage), and only if such storage: is temporary (and in no event more than 30 calendar days);您不会预取、缓存、索引或存储要在服务之外使用的任何内容,除非您可以存储有限数量的内容,但由于网络延迟(和不是为了阻止 Google 准确跟踪使用情况),并且仅当此类存储: 是临时的(并且在任何情况下都不会超过 30 个日历日); is secure;是安全的; does not manipulate or aggregate any part of the Content or Service;不操纵或聚合内容或服务的任何部分; and does not modify attribution in any way.并且不会以任何方式修改归属。

https://developers.google.com/maps/terms#10-license-restrictions https://developers.google.com/maps/terms#10-license-restrictions

At this point it looks like the ToS doesn't allow saving images on your server.此时看起来 ToS 不允许在您的服务器上保存图像。

You would first need to use the Google Places Photo API to get the Image Resource:您首先需要使用 Google Places Photo API 来获取图像资源:

https://maps.googleapis.com/maps/api/place/photo?maxwidth=[width]&photoreference=[photo_reference]&key=[apiKEY] https://maps.googleapis.com/maps/api/place/photo?maxwidth=[width]&photoreference=[photo_reference]&key=[apiKEY]

It should return on status:200 an actual image resource that you can save using:它应该在status:200返回一个实际的图像资源,您可以使用以下方法保存:

file_put_contents({{path}}, {{image resource}}); file_put_contents({{path}}, {{image resource}});

Responding to your question I try to do something similar than you but with the redirect url (the secon one).回答您的问题时,我尝试做一些与您类似的事情,但使用重定向 url(第二个)。 As you know when you wants to get a photo url from Google Places you have to send this url:如您所知,当您想从 Google 地方信息获取照片网址时,您必须发送此网址:

$url = "https://maps.googleapis.com/maps/api/place/photo?maxwidth=$maxwidth&photoreference=$reference&key=$api_key";

But then it redirects to that:但随后它重定向到:

https://lh6.googleusercontent.com/-n5VUsX_0WSA/VU3vvlOKX3I/AAAAAAAAEN8/yg9TAhSbvCwC8lA3bB56SjcUcAx3zXYtQCLIB/s1600-w800/

What I do is first get the second url and then try to get the file from cURL but the result its similar than you, an empty file (0bytes) and I tried with different formats (jpg, png and gif)我所做的是首先获取第二个 url,然后尝试从 cURL 获取文件,但结果与您相似,一个空文件(0 字节),我尝试了不同的格式(jpg、png 和 gif)

function get_photos_from_google_places($reference){
        $api_key = "YOUR API KEY";
        $maxwidth = 800;
        $url = "https://maps.googleapis.com/maps/api/place/photo?maxwidth=$maxwidth&photoreference=$reference&key=$api_key";

        $furl = false;
        // First check response headers
        $headers = get_headers($url);
        // Test for 301 or 302
        if(preg_match('/^HTTP\/\d\.\d\s+(301|302)/',$headers[0]))
        {
            foreach($headers as $value)
            {
                if(substr(strtolower($value), 0, 9) == "location:")
                {
                    $furl = trim(substr($value, 9, strlen($value)));
                }
            }
        }
        // Set final URL
        $furl = ($furl) ? $furl : $url;

        //Save the photo file to directory
        $ch = curl_init($furl);
        $fp = fopen('./files/media/image.png', 'wb');
        curl_setopt($ch, CURLOPT_FILE, $fp);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_exec($ch);
        curl_close($ch);
        fclose($fp);

        echo $furl;
        echo "<br><hr>";
        echo $fp; die();

    }

Maybe this it can gives you some idea.也许这可以给你一些想法。 If someone knows how to do it it will gratefull.如果有人知道怎么做,那将不胜感激。

Thank you very much,非常感谢,

This worked for me.这对我有用。

$query = "https://maps.googleapis.com/maps/api/place/photo?maxwidth=800&photoreference=PHOTO_REF&key=API_KEY";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $query);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$a = curl_exec($ch);
$url = Null;
if(preg_match('#Location: (.*)#', $a, $r)) {
     $url = trim($r[1]);
}

We have to aware first Google Licence Conditions :我们必须首先了解Google 许可条件

3.2.4 Restrictions Against Misusing the Services. 3.2.4 对滥用服务的限制。

No Scraping Customer will not extract, export, or otherwise scrape Google Maps Content for use outside the Services. No Scraping 客户不得提取、导出或以其他方式抓取 Google 地图内容以供在服务之外使用。 For example, Customer will not: (i) pre-fetch, index, store, reshare, or rehost Google Maps Content outside the services;例如,客户不会:(i) 在服务之外预取、索引、存储、转发或重新托管 Google 地图内容; (ii) bulk download Google Maps tiles, Street View images, geocodes, directions, distance matrix results, roads information, places information, elevation values, and time zone details; (ii) 批量下载 Google 地图图块、街景图像、地理编码、方向、距离矩阵结果、道路信息、地点信息、海拔值和时区详细信息; (iii) copy and save business names, addresses, or user reviews; (iii) 复制并保存企业名称、地址或用户评论; or (iv) use Google Maps Content with text-to-speech services.或 (iv) 将 Google 地图内容与文本转语音服务一起使用。

More Help :更多帮助:

https://cloud.google.com/maps-platform/terms/#3-license https://cloud.google.com/maps-platform/terms/#3-license

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM