0

この質問に対する答えを求めて、ここ数日探し回っていますが、求めている解決策を見つけることができませんでした。

Google_Client を使用してこのタスクを実行する完全に機能するバージョンがありますが、Google_Client を使用せずにこれを実行できるようにしたいと考えています。バケットを使用せずに作成できますが、オブジェクトは作成できません。これに関するGoogleのドキュメントをまったく理解できません(かなり貧弱です)。

そのため、いくつかのパラメーターを取り込んでファイルをアップロードしようとする関数を以下に示します。$authheaders$post_fields(投稿の本文)またはURLに何を入れる必要があるのか​​ わかりません。

public function upload_file($bucket, $fileName, $file, $fileType) {

        // Check if we have auth token
        if(empty($this->authtoken)) {
            echo "Please login to Google";
            exit;
        }

        // Prepare authorization headers
        $authheaders = array(
            "Authorization: Bearer " . $this->authtoken
        );

        $postbody = array();

        //Http call for creating a file
        $response = $this->http_call(self::FILE_UPLOAD_URL.$bucket.'/o?uploadType=multipart&name='.$fileName, $postbody, $authheaders);

        // Has the file been created successfully?
        if($response->success=="1") {
            return array('status' => 'success', 'errorcode' =>'', 'errormessage'=>"", 'id' => $response->job->id);
        }
        else {

            return $response;
        }
}

http_call 関数:

public function http_call($url, $post_fields, $authheaders) {

        // Make http call
        $this->httpRequest->setUrl($url);
        $this->httpRequest->setPostData(json_encode($post_fields));
        $this->httpRequest->setHeaders($authheaders);
        $this->httpRequest->send();
        $response = json_decode($this->httpRequest->getResponse());

        return $response;

}

これに関するヘルプは大歓迎です。

4

1 に答える 1

1

APIを使用せずにこれを実行しようとしている人がいる場合は、curlを使用して質問を解決した方法を次に示します.

// Prepare authorization headers
        $authheaders = array(
            "Authorization: Bearer " . $this->authtoken,
            "Content-Type: application/pdf"
        );

        $url = self::FILE_UPLOAD_URL.$bucket.'/o/?uploadType=multipart&name='.$fileName.'';

        $curl = curl_init();

        curl_setopt($curl, CURLOPT_POSTFIELDS, $file);

        curl_setopt($curl, CURLOPT_HTTPHEADER, $authheaders);
        curl_setopt($curl, CURLOPT_URL, $url);

        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($curl, CURLOPT_USERAGENT, "Goole Cloud Storage PHP Starter Application google-api-php-client/1.1.5");

        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
        // 1 is CURL_SSLVERSION_TLSv1, which is not always defined in PHP.
        curl_setopt($curl, CURLOPT_SSLVERSION, 1);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HEADER, true);


        $response = curl_exec($curl);
于 2015-09-21T12:33:06.803 に答える