脚本宝典收集整理的这篇文章主要介绍了php – 使用curl从Google Places API保存照片副本,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
根据Google API documentation的请求格式如下:
https://maps.googleapis.COM/maps/api/place/photo?maxwidth=400&photoreference=CoQBegAAAFg5U0y-iQEtUVMfqw4KpXYe60QwJC-wl59NZlcaxSQZNgAhGrjmUKD2NkXatfQF1QRap-PQCx3kMfsKQCcxtkZQQ&sensor=true&key=AddYourOwnKeyHere
所以我尝试了这个功能:
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");
..其中$photo保存请求网址.
这不起作用,它保存带有标题错误的空图像,这可能是因为请求不是照片的实际网址.此外,在请求网址中,我不可能知道我将获得哪个图像扩展(jpg,png,gif等),这是另@L_777_4@问题.
有关如何保存照片的任何帮助表示赞赏.
它提供了一个非常有用的函数,可以在重定向后返回实际的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),9) == "location:") { $furl = trim(substr($value,9,strlen($value))); } } } // Set final URL $furl = ($furl) ? $furl : $url; return $furl; }
因此,您将Google Place Photo请求uRL传递给此函数,它会在重定向后返回照片的实际网址,然后可以与CURL一起使用.它还解释了有时候,卷曲选项curl_setopt($ch,1);并不总是有效.
以上是脚本宝典为你收集整理的php – 使用curl从Google Places API保存照片副本全部内容,希望文章能够帮你解决php – 使用curl从Google Places API保存照片副本所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。