0

I have a script from Google drive and wordks fine when i executing by browser, but i want to execute it by Wget, but that doesn't work. Why does this not work with 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'
  )
);
Legionar
  • 7,472
  • 2
  • 41
  • 70
Bas
  • 2,330
  • 4
  • 29
  • 68
  • Wget just downloads a file, it doesn't execute it. – Oldskool Nov 05 '15 at 14:06
  • @Oldskool what do i need? – Bas Nov 05 '15 at 14:08
  • Just use the PHP interpreter to run it: `php yourfile.php`. – Oldskool Nov 05 '15 at 14:09
  • @Oldskool, you could probably technically run this using `wget`. You can send headers and save state using sessions and cookies. Theoretically anything you can view on a browser should be viewable via `wget`, it just is probably not worth the added complication. – smcjones Nov 05 '15 at 14:15

1 Answers1

0

wget pulls what you'd receive if you navigated to a website yourself. That means that you'd receive whatever you'd normally receive were you to put the link into the browser yourself without any session information.

You can try wget with --save-cookies. Based on how OAuth works, you'll probably also need --header, and quite possibly --referer (for details see here. For an example see this SO post.)

This is complicated and is essentially creating a client wrapper for a PHP script. There is a better way.

You can execute a PHP file on a command line by using php [your php script] (as per @Oldskool's comment). Note that this may not work.

You can also download a client for bash or PowerShell.

Community
  • 1
  • 1
smcjones
  • 5,490
  • 1
  • 23
  • 39
  • i am getting this error by php /home/xxxxx/public_html/simple-file-upload.php Status: 500 Internal Server Error X-Powered-By: PHP/5.4.45 Set-Cookie: PHPSESSID=tjr28i2fahu1phtmo6blhp9ov3; path=/ – Bas Nov 05 '15 at 14:29
  • **This is complicated and is essentially creating a client wrapper for a PHP script. There is a better way.**. I am warning you against this route. There are far too many conditionals and it's a rabbit hole not worth going down when there are already alternative solutions. If you can't do it one way, try it another. Unless you're doing it solely to learn, in which case, have fun and keep trying! – smcjones Nov 05 '15 at 14:32
  • If you're having trouble with executing using `php` then I would strongly recommend downloading a client for the job. If you're running linux, you can download the one for bash (following my link). If you're running Windows, you can download the one for PowerShell. – smcjones Nov 05 '15 at 14:34
  • I'm telling you, **this is a lot more complicated than it appears** which is why Google's been hard at work creating a linux solution for years. They have one, which I've linked to. It's the native way and skips all the confusing parts. You can run a cron job off of it. – smcjones Nov 05 '15 at 14:41