节省http://maps.googleapis.com/maps/api/streetview?vars用PHP卷曲,或的fsockopen的fopen

问题描述:

对于应用程序i'me建筑物图像我想从http://maps.googleapis.com/maps/api/streetview?location=myaddressvariable &使用街景图像大小= mysizes &键= myapikey & restofvars ...节省http://maps.googleapis.com/maps/api/streetview?vars用PHP卷曲,或的fsockopen的fopen

这让这个形象trought一个正常的浏览器请求时工作正常。

但我真的很想用php - > cUrl或fsockopen或fopen保存这个图像。 奇怪的是,无论我使用请求超时的PHP功能,我从谷歌得到400错误。

这是一个权限问题还是我做错了什么?

那么什么是最好的方式来保存图像从谷歌街景使用服务器端请求与PHP?

+0

刮到谷歌页面是违反他们的服务条款。 –

这就是我的做法。做任何你想做的事情,你可能需要编辑它。我也使用缓存。

public function Flat($Coors='', $Zoom=15, $Size='250x200'){ 
    # Ok, on doit avoir des coordonnées valides 
    $Coors = array_map('trim', explode(',', $Coors)); 

    $CoorsName = array(); 
    foreach($Coors as $X){ 
     if(!is_numeric($X)){ 
      $Error = 1; 
     } else { 
      if($X[0] == '-'){ 
       $CoorsName[] = substr($X, 0, 6); 
      } else { 
       $CoorsName[] = substr($X, 0, 5); 
      } 
     } 
    } 
    $Coors = implode(',', $Coors); 
    $CoorsName = implode('_', $CoorsName); 

    # Le zoom doit aussi être valide 
    if(!is_numeric($Zoom) OR !is_int($Zoom)){ 
     $Error = 1; 
    } 

    # Maintenant on doit s'assurer de la grandeur 
    $Size = array_map('trim', explode('x', $Size)); 

    foreach($Size as $X){ 
     if(!is_numeric($X)){ 
      $Error = 1; 
     } 
    } 
    $Size = implode('x', $Size); 

    # Ok, avant d'aller chercher l'image, on doit vérifier le cache 
    $ImageExist = false; 

    # Est-ce que les dossiers existes ? 
    $CachePath = '/home/.../cache/maps/flat/'; 
    $PathLevel = array($CachePath.$Zoom.'/', 
         $CachePath.$Zoom.'/'.$Size.'/', 
         $CachePath.$Zoom.'/'.$Size.'/'.$CoorsName.'/'); 

    foreach($PathLevel as $Path){ 
     if(!file_exists($Path)){ 
      mkdir($Path, 0777); 
     } 
    } 

    # Chemin complet de l'image 
    $ImagePath = $PathLevel[2].$Coors.'.png'; 
    $GoogleURL = 'http://maps.googleapis.com/maps/api/staticmap?'; 
    $Args = array( 'center' => $Coors, 
        'zoom'  => $Zoom, 
        'size'  => $Size, 
        'markers' => 'icon:http://www.domain.com/pin.png|'.$Coors, 
        'sensor' => 'false'); 

    $URL_GET = ''; 
    foreach($Args as $Arg => $Value){ 
     $URL_GET.= ($URL_GET != '' ? '&' : '') . $Arg.'='.$Value.'&'; 
    } 
    $GoogleURL.= ($URL_GET != '' ? '?'.$URL_GET : ''); 

    # On va vérifier si l'image existe maintenant 
    if(!file_exists($ImagePath)){ 
     $Content = file_get_contents($GoogleURL); 

     $fHandle = fopen($ImagePath, 'a'); 
     fwrite($fHandle, $Content); 
     fclose($fHandle); 

    // file_put_contents($ImagePath, $Content); 
    } else { 
     $ImageExist = true; 
    } 

    # Header requis 
    header('Pragma: public'); // Requis 
    header('Expires: 0'); 
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
    header('Cache-Control: private', false); // Requis 
    header('Content-Type: image/png'); 
    header('Content-Transfer-Encoding: binary'); 

    # On va afficher l'image maintenant 
    if($ImageExist === true){ 
     header('Content-Length: '.filesize($ImagePath)); 
     readfile($ImagePath); 
    } else { 
     header('Content-Length: '.strlen($Content)); 
     echo $Content; 
    } 
} 
+0

感谢有用的功能,我发现我不得不用%20替换所有的空间。由于这是一个Ajax请求,400邮件是因为网址不正确。 – user1483639

+0

我的荣幸。至少,你发现了这个问题。 –