街景发布API PHP - 上传元数据后无法找到上传参考

时间:2017-10-17 03:34:12

标签: php file-upload upload google-api-php-client google-streetview-publish

我使用PHP发布全景图像。我在Google Document中执行了3个步骤并在上传的元数据后成功收到了PhotoID,但是当我将这些photoID用于其他请求时,它返回"无法找到上传参考。请确保您已将文件上传到上传参考网址。如果此错误仍然存​​在,请申请新的上传网址,然后重试“#34;。

这是我的代码:

获取上传网址

$cur_upload_url = curl_init();
  curl_setopt_array($cur_upload_url, array(
   CURLOPT_URL => "https://streetviewpublish.googleapis.com/v1/photo:startUpload?key=$api_key",
   CURLOPT_RETURNTRANSFER => true,
   CURLOPT_ENCODING => "" ,
   CURLOPT_CUSTOMREQUEST => "POST",
   CURLOPT_HTTPHEADER => array(
     "authorization: Bearer $access_token",
        "content-type: application/json",
        "Content-Length: 0"
        ), 
  ));
  $response = curl_exec($cur_upload_url);
  echo $response;
  $re = '/https?:\/\/[^"]*/';
  $str = $response;
  preg_match($re, $str, $matches, PREG_OFFSET_CAPTURE, 0);
  $upload_url = $_SESSION['UploadRef'] = $matches[0][0];

回复:

{  
   "uploadUrl":"https://streetviewpublish.googleapis.com/media/user/104039888676357869012/photo/2857577503984652262"
}

将照片上传到上传网址

$cmd = exec("curl --request POST \
        --url '$upload_url' \
        --upload-file '$imagePath' \
        --header 'Authorization: Bearer $access_token'"
    , $outputAndErrors, $return_value);

它没有任何回报。

上传元数据

$curl_meta = curl_init();
  curl_setopt_array($curl_meta, array(
    CURLOPT_URL => "https://streetviewpublish.googleapis.com/v1/photo?key=$api_key",
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POSTFIELDS => '{
                    "uploadReference":
                    {
                      "uploadUrl": "'.$upload_url.'"
                    },
                    "pose":
                     {
                       "heading": 95.0,
                       "latLngPair":
                       {
                         "latitude": '.$latVal.',
                         "longitude": '.$langVal.'
                       }
                    },
                    "captureTime":
                    {
                      "seconds":  '.$time_stamp.'
                    },
                  }',
    CURLOPT_HTTPHEADER => array(
       "authorization: Bearer $access_token",
       "content-type: application/json"
    ),
  ));
      $response_meta = curl_exec($curl_meta);

响应

{  
   "photoId":{  
      "id":"CAoSLEFGMVFpcE4wTDEycFl6S2xVOWtUWmlRVHZCSm90bHp6QUpRWVZ5QlNoWnF4"
   }
}

当我尝试更新连接或运行photo.create API时:

$curl_meta = curl_init();
  curl_setopt_array($curl_meta, array(
    CURLOPT_URL => "https://streetviewpublish.googleapis.com/v1/photo?key=$api_key",
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POSTFIELDS => '{
  "photoId": {
    "id": "'.$photoID.'"
  },
  "uploadReference": {
    "uploadUrl": "'.$upload_url.'"
  },
  "captureTime": "'.(new DateTime())->format('Y-m-d\TH:i:s\Z').'",
  "connections": [],
  "places": [],
  "pose": {
    "heading": 0
  }
}',
    CURLOPT_HTTPHEADER => array(
       "authorization: Bearer $access_token",
       "content-type: application/json"
    ),
  ));
      $response_meta = curl_exec($curl_meta);

它回应

{  
   "error":{  
      "code":404,
      "message":"The upload reference cannot be found. Please make sure you have uploaded a file to the upload reference URL. If this error persists, request a new upload URL and try again.",
      "status":"NOT_FOUND"
   }
}

使用返回的photoID获取照片

exec('curl --request GET \
--url "'. addslashes('https://streetviewpublish.googleapis.com/v1/photo/'.$photoID.'?key='.$api_key) .'" \
--header "Authorization: Bearer '. addslashes($access_token) .'" ',
$outputAndErrors, $return_value);

响应

"error":{  
      "code":404,
      "message":"Image not found for id: CAoSLEFGMVFpcE9faE52aG95TTYtaENjd1NRX3BCU2l4czcwVnVXQS1jd3dxMGxO",
      "status":"NOT_FOUND"
   }

我很困难,需要找到解决方案。

非常感谢!

1 个答案:

答案 0 :(得分:1)

我还使用Try this API部分尝试了您的请求,但遇到了同样的错误。正如我所观察到的,photo.create用于发布上传的照片,就像3. Upload the metadata of the photo一样。当我重试并删除photoId参数时,

{
 "uploadReference": {
   "uploadUrl": "https://streetviewpublish.googleapis.com/media/user/1234567890/photo/1234567890"
 },
 "connections": [],
 "places": [],
 "pose": {},
}

我成功获得了200和photoID(您使用3. Upload the metadata of the photo收到的相同输出)。

enter image description here

关于"使用返回的photoID"获取照片,我认为没有必要在您的请求中添加addslashes

$ curl --request GET \
--url 'https://streetviewpublish.googleapis.com/v1/photo/PHOTO_ID?key=YOUR_API_KEY' \
--header 'authorization: Bearer YOUR_ACCESS_TOKEN'