2015-11-05 220 views
0

我从谷歌的脚本驱动,当我通过浏览器执行wordks很好,但我想通过Wget的执行,但不起作用。 为什么这不适用于wget?插入文件谷歌驱动的wget

$client = new Google_Client(); 
$client->setAuthConfig($oauth_credentials); 
$client->setRedirectUri($redirect_uri); 
$client->addScope("https://www.googleapis.com/auth/drive"); 
$service = new Google_Service_Drive($client); 

if (!empty($_SESSION['upload_token'])) { 
    $client->setAccessToken($_SESSION['upload_token']); 
    if ($client->isAccessTokenExpired()) { 
    unset($_SESSION['upload_token']); 
    } 
} 
else { 
    $authUrl = $client->createAuthUrl(); 
} 

DEFINE("TESTFILE", 'testfile-small.txt'); 

if (!file_exists(TESTFILE)) { 
    $fh = fopen(TESTFILE, 'w'); 
    fseek($fh, 1024 * 1024); 
    fwrite($fh, "!", 1); 
    fclose($fh); 
} 

// This is uploading a file directly, with no metadata associated. 
$file = new Google_Service_Drive_DriveFile(); 

$result = $service->files->insert(
    $file, 
    array(
    'data' => file_get_contents(TESTFILE), 
    'mimeType' => 'application/octet-stream', 
    'uploadType' => 'media' 
) 
); 
+0

Wget的只是下载一个文件,它不执行它。 – Oldskool

+0

@Oldskool我需要什么? – Bas

+0

只需使用PHP解释器来运行它:'php yourfile.php'。 – Oldskool

回答

0

wget如果您自己导航到网站,将会收到您将收到的内容。这意味着,如果您没有任何会话信息,您将自己的链接放入浏览器,您就会收到通常会收到的任何内容。

你可以尝试用wget--save-cookies。基于OAuth的是如何工作的,你可能还需要--header,而且很有可能--referer(详见here。举一个例子看this SO post。)

这是复杂的,本质上是创建一个PHP脚本客户包装。有一个更好的方法

您可以通过使用php [your php script](按@ OLDSKOOL的评论)在命令行上执行PHP文件。请注意,这可能无法正常工作。

您也可以下载一个客户端bashPowerShell

+0

我得到这个错误通过php /home/xxxxx/public_html/simple-file-upload.php状态:500内部服务器错误 X-Powered-By:PHP/5.4.45 Set-Cookie:PHPSESSID = tjr28i2fahu1phtmo6blhp9ov3;路径=/ – Bas

+0

**这很复杂,实际上是为PHP脚本创建一个客户端封装器。有一个更好的方法。**。我警告你不要这样。有太多的条件,这是一个兔子洞,当已经有其他解决方案的时候,这个兔子洞是不值得的。如果你无法做到这一点,请尝试另一种方式。除非你只是为了学习而做,在这种情况下,玩得开心,并继续努力! – smcjones

+0

如果您在使用'php'那么我强烈建议下载一个客户端,作业执行的麻烦。如果你正在运行linux,你可以下载bash(在我的链接之后)。如果您正在运行Windows,则可以下载PowerShell。 – smcjones