无声地无法通过PHP SDK将图像推送到用户的Google云端硬盘

时间:2018-10-31 19:58:07

标签: php google-api google-drive-api google-api-php-client google-php-sdk

我正在制作一个开源应用程序,用户可以在其中使用PHP SDK将其Facebook相册备份到Google驱动器。我已经完成了Facebook部分,现在却停留在google的书面文档中。所以这是我逐步使用的方式:

用户身份验证::我所有的请求和回调都在一个PHP文件中进行管理,以寻求类似于API的工作方式。因此,这是处理将文档上传到google的请求的代码块:

<?php
session_start();
require_once __DIR__.'/classes/facebook.class.php';
require_once __DIR__.'/classes/drive.class.php';
$fb = new Facebook();
$google = Drive::getInstance();
$query = $_REQUEST['i'];
switch($query){
        case "backup_req":
            $agent = $google->getAgent();
            if(!isset($_SESSION['google_access_token'])){
                $_SESSION['album_temp'] = $_REQUEST['album'];
                header("location: ".filter_var($agent->createAuthUrl(), FILTER_SANITIZE_URL));
            } else{
                $google->uploadAlbum($fb, $_REQUEST['album']);
            }
        break;
        case "google_callback":
            $agent = $google->getAgent();
            if(!isset($_SESSION['google_access_token'])){
                $code = $_GET['code']; // just to keep the code neat.. no need to store in other var!
                $res = $google->getAgent()->authenticate($code); // hope this works
                $_SESSION['google_access_token'] = $agent->getAccessToken();
            }
            $google->uploadAlbum($fb, $_SESSION['album_temp']);
        break;
}

这是我想做的,如果会话中没有google的访问令牌,我们会将用户重定向到google的登录页面。完成后会重定向到同一文件,但是这次请求google_callback和代码。然后将其存储到会话中,然后继续进行uploadAlbum方法。

注意,我已经回显了服务器的内容,是的,我确实获得了访问令牌和刷新令牌

现在,要上传相册,Drive类中有4种方法(单例)。这是代码:

    class Drive{
    private static $instance;
    private $fileRequest;
    private $mimeType;
    private $filename;
    private $path;
    private $client;
    private $clientId = "<client-id>";
    private $clientSecret = "<client-secret>";
    private final function __construct(){
        $this->client = new Google_Client();
        $this->client->setApplicationName("fb album backup tool");
        $this->client->setClientId($this->clientId);
        $this->client->setClientSecret($this->clientSecret);
        $this->client->setRedirectUri("https://fbrtc.sameer-manek.com/fb_caller.php?i=google_callback");
        $this->client->setScopes(array('https://www.googleapis.com/auth/drive.file'));
        $this->client->setAccessType("offline");
        $this->client->setApprovalPrompt('force');
    }
    public static function getInstance(){
        if(self::$instance == null) {
            self::$instance = new Drive();
        }
        return self::$instance;
    }
    public function getAgent(){
        return $this->client;
    }
    public function uploadAlbum($fb, $album){
            $nodes = $fb->get_photos($album);
            $client = new GearmanClient();
            $client->addServer();
            foreach ($nodes as $node) {
                //$client->addTask('init', $node['picture']);
                try{
                    $data = file_get_contents($node['picture']);
                    $saveto = __DIR__."/../scripts/tmp/".rand().".jpg";
                    $file = fopen($saveto, "w+");
                    fwrite($file, $data);
                    fclose($file);
                    $this->init($saveto);
                    echo "uploaded ".$saveto."\n";
                } catch(Exception $e) {
                    return false;
                }

            }
            return true;
            //$client->runTasks();
    }
    public function init($file){
        $at = $_SESSION['google_access_token']['access_token'];
        $this->client->setAccessToken($at);
        $this->fileRequest = $file;
        $client = $this->client;
        //$client->refreshToken($_SESSION['google_access_token']['refreshToken']);
        $tokens = $client->getAccessToken();
        $client->setAccessToken($tokens);
        $client->setDefer(true);
        $this->client->setAccessToken($at);
        $this->processFile();
    }
    public function processFile(){
        $fileRequest = $this->fileRequest;
        $path_parts = pathinfo($this->fileRequest);
        $this->path = $path_parts['dirname'];
        $this->fileName = $path_parts['basename'];
        $finfo = finfo_open(FILEINFO_MIME_TYPE);
        $this->mimeType = finfo_file($finfo, $fileRequest);
        finfo_close($finfo);
        $this->upload();
    }
    public function upload(){
        $client = $this->client;
        $service = new Google_Service_Drive($this->client);
        //Insert a file
        $file = new Google_Service_Drive_DriveFile();
        $file->setName($this->fileName.'.jpg');
        $file->setDescription('A test document');
        $file->setMimeType('image/jpeg');
        $data = file_get_contents($this->fileRequest);
        $createdFile = $service->files->create($file, array(
              'data' => $data,
              'mimeType' => 'image/jpeg',
              'uploadType' => 'multipart'
        ));
    }
}

uploadAlbum()方法正在从请求的相册中获取图像。 init()方法正在准备对象以上传该图像(此处,我们为对象使用分配访问令牌) processFile()方法正在获取和存储有关图像的信息。 upload()方法实际上是将图像上传到用户的Google驱动器。

执行此操作后,没有生成任何异常,也没有生成任何错误,但是当我进行交叉检查时,图像没有上载到驱动器。

我无法指出问题所在,请帮助我解决此错误。谢谢。

[EDIT]我var_dump $createdFile的内容,并在屏幕上以下内容转储:

object(GuzzleHttp\Psr7\Request)#73 (7) { ["method":"GuzzleHttp\Psr7\Request":private]=> string(4) "POST" ["requestTarget":"GuzzleHttp\Psr7\Request":private]=> NULL ["uri":"GuzzleHttp\Psr7\Request":private]=> object(GuzzleHttp\Psr7\Uri)#69 (7) { ["scheme":"GuzzleHttp\Psr7\Uri":private]=> string(5) "https" ["userInfo":"GuzzleHttp\Psr7\Uri":private]=> string(0) "" ["host":"GuzzleHttp\Psr7\Uri":private]=> string(18) "www.googleapis.com" ["port":"GuzzleHttp\Psr7\Uri":private]=> NULL ["path":"GuzzleHttp\Psr7\Uri":private]=> string(22) "/upload/drive/v3/files" ["query":"GuzzleHttp\Psr7\Uri":private]=> string(20) "uploadType=multipart" ["fragment":"GuzzleHttp\Psr7\Uri":private]=> string(0) "" } ["headers":"GuzzleHttp\Psr7\Request":private]=> array(3) { ["Host"]=> array(1) { [0]=> string(18) "www.googleapis.com" } ["content-type"]=> array(1) { [0]=> string(37) "multipart/related; boundary=344323595" } ["X-Php-Expected-Class"]=> array(1) { [0]=> string(30) "Google_Service_Drive_DriveFile" } } ["headerNames":"GuzzleHttp\Psr7\Request":private]=> array(3) { ["content-type"]=> string(12) "content-type" ["host"]=> string(4) "Host" ["x-php-expected-class"]=> string(20) "X-Php-Expected-Class" } ["protocol":"GuzzleHttp\Psr7\Request":private]=> string(3) "1.1" ["stream":"GuzzleHttp\Psr7\Request":private]=> object(GuzzleHttp\Psr7\Stream)#67 (7) { ["stream":"GuzzleHttp\Psr7\Stream":private]=> resource(11) of type (stream) ["size":"GuzzleHttp\Psr7\Stream":private]=> NULL ["seekable":"GuzzleHttp\Psr7\Stream":private]=> bool(true) ["readable":"GuzzleHttp\Psr7\Stream":private]=> bool(true) ["writable":"GuzzleHttp\Psr7\Stream":private]=> bool(true) ["uri":"GuzzleHttp\Psr7\Stream":private]=> string(10) "php://temp" ["customMetadata":"GuzzleHttp\Psr7\Stream":private]=> array(0) { } } } uploaded /sites/facebook_backup/classes/../scripts/tmp/264708337.jpg

这是指向GitHub repo的链接以供参考。

0 个答案:

没有答案