google streetview publish api:使用javascript来startUpload()

时间:2018-06-22 14:59:30

标签: javascript google-streetview-publish

使用Google Streetview发布api,经过正确的身份验证后,我成功列出了照片,但是我被startUpload()困住了。

gapi.client.streetviewpublish.photo.startUpload().then(function(resp) {
     var upload_url = resp.result.uploadUrl;
     console.log( "publish url = " + upload_url );                          
});

这会正确返回上载网址,但此后我找不到如何发送图像字节的信息……有人有任何线索吗?

感谢帮助!

编辑: 解决方案是使用ajax请求将vars发布到PHP:

$upload_url = isset($_POST['upload_url']) ? $_POST['upload_url'] : NULL;
$access_token = isset($_POST['bearer']) ? $_POST['bearer'] : NULL;
$action =  isset($_POST['action']) ? $_POST['action'] : NULL;
$imagePath = isset($_POST['imagePath']) ? $_POST['imagePath'] : NULL;

if($action==="sendbytes"){

        $fp = fopen( $imagePath, 'r' );
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $upload_url );
        curl_setopt($ch, CURLOPT_UPLOAD, 1);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");  
        curl_setopt($ch, CURLOPT_INFILE, $fp);
        curl_setopt($ch, CURLOPT_NOPROGRESS, false);
        curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, 'CURL_callback');
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                     
          'authorization: Bearer '.$access_token,
          'Content-Type: image/jpeg',                                             
          'Content-Length: ' . filesize( $imagePath ))                                     
        );  
        curl_setopt($ch, CURLOPT_INFILESIZE, filesize( $imagePath ));
        curl_exec ($ch);

        if (curl_errno($ch)) {
            $msg = curl_error($ch);
        } else {
            $msg = 'File uploaded successfully.';
        }
        curl_close ($ch);

        echo $msg;      
}

0 个答案:

没有答案