简体   繁体   English

保存来自 Google Places 的照片副本 API Place Photos using curl

[英]Save copy of a photo from Google Places API Place Photos using curl

I'm trying to grab a photo from Google Place Photos using curl and save it on my server.我正在尝试使用 curl 从 Google Place Photos 抓取一张照片并将其保存在我的服务器上。

The request format as per the Google API documentation is like this:根据Google API 文档的请求格式如下:

https://maps.googleapis.com/maps/api/place/photo?maxwidth=400&photoreference=CoQBegAAAFg5U0y-iQEtUVMfqw4KpXYe60QwJC-wl59NZlcaxSQZNgAhGrjmUKD2NkXatfQF1QRap-PQCx3kMfsKQCcxtkZqQ&sensor=true&key=AddYourOwnKeyHere

So I tried this function:所以我尝试了这个 function:

function download_image1($image_url, $image_file){
    $fp = fopen ($image_file, 'w+');
    $ch = curl_init($image_url);
    // curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // enable if you want
    curl_setopt($ch, CURLOPT_FILE, $fp); // output to file
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 1000); // some large value to allow curl to run for a long time
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0');
    // curl_setopt($ch, CURLOPT_VERBOSE, true); // Enable this line to see debug prints
    curl_exec($ch);
    curl_close($ch); // closing curl handle
    fclose($fp); // closing file handle
}

download_image1($photo, "test.jpg");

..where $photo holds the request url. ..其中$photo包含请求 url。

This is not working, it saves an empty image with header errors, it probably is because the request is not the actual url of the photo.这不起作用,它保存了一个带有 header 错误的空图像,这可能是因为请求不是照片的实际 url。 Also, in the request url, it's not possible to know which image extension I'm going to get (jpg, png, gif, etc) so that's another problem.此外,在请求 url 中,不可能知道我将获得哪个图像扩展名(jpg、png、gif 等),所以这是另一个问题。

Any help on how to save the photos appreciated.任何有关如何保存照片的帮助表示赞赏。

EDIT: I get the header errors "Can't read file header" in my image viewer software when I try to open the image.编辑:当我尝试打开图像时,我的图像查看器软件出现 header 错误“无法读取文件头”。 The script itself doesn't show any errors.脚本本身没有显示任何错误。

I found a solution here: http://kyleyu.com/?q=node/356 我在这里找到了一个解决方案: http//kyleyu.com/?q = node / 356

It gives a very useful function to return the actual URL after redirection: 它提供了一个非常有用的函数,可以在重定向后返回实际的URL:

function get_furl($url)
    {
    $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;
    return $furl;
    }

So you pass the Google Place Photo request uRL to this function and it returns the actual URL of the photo after the redirection which then can be used with CURL. 因此,您将Google Place Photo请求uRL传递给此函数,它会在重定向后返回照片的实际网址,然后可以与CURL一起使用。 It also explains that sometimes, the curl option curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 它还解释了有时候,卷曲选项curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); doesn't always work. 并不总是有效。

https://stackoverflow.com/a/23540352/2979237 https://stackoverflow.com/a/23540352/2979237

We can minimize the above code to change as adding 1 as the second parameter of get_header() function like $headers = get_headers($url, 1);我们可以将上面的代码最小化,改成get_header()的第二个参数加1 function like $headers = get_headers($url, 1); . . that will return the associate values.这将返回关联值。

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

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