title: Converting curl in bash to php
body: <p>Well, here's my cURL script inside bash that works without any issues!</p>
  
  <pre><code>#!/bin/bash
  fileid="1yvklOFopnep8twiqAQecmMUoAbQVzU0r"
  filename="MyFile.mp4"
  curl -c ./cookie -s -L "https://drive.google.com/uc?export=download&amp;id=${fileid}" &gt; /dev/null
  curl -Lb ./cookie "https://drive.google.com/uc?export=download&amp;confirm=`awk '/download/ {print $NF}' ./cookie`&amp;id=${fileid}" -o ${filename}
  </code></pre>
  
  <p>However I'm trying to rewrite this into a simple PHP script, although it appears not to be working correctly; here's the code:</p>
  
  <pre><code>&lt;?php
  
      define('FILENAME', 'MyFile.mp4');
      define('FILE_ID', '1yvklOFopnep8twiqAQecmMUoAbQVzU0r');
  
      $GlobalFileHandle = null;
  
      function get_confirm($id)
      {
          $ch = curl_init();
          curl_setopt($ch, CURLOPT_URL, "https://drive.google.com/uc?export=download&amp;id=".$id);
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
          curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
          curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
          $result = curl_exec($ch);
          if (curl_errno($ch)) {
              echo 'Error:' . curl_error($ch);
          }
          curl_close($ch);
          preg_match_all("/confirm=([0-9A-Za-z]+)&amp;/", $result, $output_array);
          return $output_array[1][0];
      }
  
      function get_file($id, $confirm)
      {
          global $GlobalFileHandle;
          $GlobalFileHandle = fopen(FILENAME, 'w+');
          $ch = curl_init();
          curl_setopt($ch, CURLOPT_URL, 'https://drive.google.com/uc?export=download&amp;confirm='.$confirm.'&amp;id='.$id);
          curl_setopt($ch, CURLOPT_FILE, $GlobalFileHandle);
          curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
          curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
          curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'curlWriteFile');
          curl_exec($ch);
          if (curl_errno($ch)) {
              echo 'Error:' . curl_error($ch);
          }
          curl_close($ch);
          fclose($GlobalFileHandle);
      }
  
      function curlWriteFile($cp, $data)
      {
          global $GlobalFileHandle;
          return fwrite($GlobalFileHandle, $data);
      }
  
      $confirm_code = get_confirm(FILE_ID);
  
      echo "We got our confirm code! ".$confirm_code;
  
      get_file(FILE_ID, $confirm_code);
  
  </code></pre>
  
  <p>However it appears the file is not being downloaded &amp; the MyFile.mp4 remains empty?</p>