title: PHP webhook script (bitbucket)
body: <p>I'm trying to attempt using a webhook script so that I can just commit locally and have the script triggered server-side and pull in any change. </p>
  
  <p>Now if I login to the server via SSH and run <code>php webhook.php</code> the cript is triggered successfully and the files are updated. So I know the file does work. But if I make edits to the files, commit and push to master, I'm not seeing the files updated. </p>
  
  <p>The log file produced by the script suggests everything is fine, but clearly it's not. </p>
  
  <p>My file structure is like this:</p>
  
  <pre><code>var
    www
      my-project
      - webhook.php
         repo-folder
  </code></pre>
  
  <p>So the file should be pulling the files into repo-folder and the webhook.php is set as the webhook via the bitbucket control panel. If I view the log within bitbucket, it's showing a successful request every time I push a commit. </p>
  
  <p>The script:</p>
  
  <pre><code>&lt;?php
  
  date_default_timezone_set('Europe/London');
  
  class Deploy {
  
      /**
       * A callback function to call after the deploy has finished.
       *
       * @var callback
       */
      public $post_deploy;
  
      /**
       * The name of the file that will be used for logging deployments. Set to
       * FALSE to disable logging.
       *
       * @var string
       */
      private $_log = 'deployments.log';
  
      /**
       * The timestamp format used for logging.
       *
       * @link    http://www.php.net/manual/en/function.date.php
       * @var     string
       */
      private $_date_format = 'Y-m-d H:i:sP';
  
      /**
       * The name of the branch to pull from.
       *
       * @var string
       */
      private $_branch = 'master';
  
      /**
       * The name of the remote to pull from.
       *
       * @var string
       */
      private $_remote = 'origin';
  
      /**
       * The directory where your website and git repository are located, can be
       * a relative or absolute path
       *
       * @var string
       */
      private $_directory;
  
      /**
       * Sets up defaults.
       *
       * @param  string  $directory  Directory where your website is located
       * @param  array   $data       Information about the deployment
       */
      public function __construct($directory, $options = array())
      {
          // Determine the directory path
          $this-&gt;_directory = realpath($directory).DIRECTORY_SEPARATOR;
  
          $available_options = array('log', 'date_format', 'branch', 'remote');
  
          foreach ($options as $option =&gt; $value)
          {
              if (in_array($option, $available_options))
              {
                  $this-&gt;{'_'.$option} = $value;
              }
          }
  
          $this-&gt;log('Attempting deployment...');
      }
  
      /**
       * Writes a message to the log file.
       *
       * @param  string  $message  The message to write
       * @param  string  $type     The type of log message (e.g. INFO, DEBUG, ERROR, etc.)
       */
      public function log($message, $type = 'INFO')
      {
          if ($this-&gt;_log)
          {
              // Set the name of the log file
              $filename = $this-&gt;_log;
  
              if ( ! file_exists($filename))
              {
                  // Create the log file
                  file_put_contents($filename, '');
  
                  // Allow anyone to write to log files
                  chmod($filename, 0666);
              }
  
              // Write the message into the log file
              // Format: time --- type: message
              file_put_contents($filename, date($this-&gt;_date_format).' --- '.$type.': '.$message.PHP_EOL, FILE_APPEND);
          }
      }
  
      /**
       * Executes the necessary commands to deploy the website.
       */
      public function execute()
      {
          try
          {
              // Make sure we're in the right directory
              chdir($this-&gt;_directory);
              $this-&gt;log('Changing working directory... ');
  
              // Discard any changes to tracked files since our last deploy
              exec('git reset --hard HEAD', $output);
              $this-&gt;log('Reseting repository... '.implode(' ', $output));
  
              // Update the local repository
              exec('git pull '.$this-&gt;_remote.' '.$this-&gt;_branch, $output);
              $this-&gt;log('Pulling in changes... '.implode(' ', $output));
  
              // Secure the .git directory
              exec('chmod -R og-rx .git');
              $this-&gt;log('Securing .git directory... ');
  
              if (is_callable($this-&gt;post_deploy))
              {
                  call_user_func($this-&gt;post_deploy, $this-&gt;_data);
              }
  
              $this-&gt;log('Deployment successful.');
          }
          catch (Exception $e)
          {
              $this-&gt;log($e, 'ERROR');
          }
      }
  
  }
  
  // This is just an example
  $deploy = new Deploy('/var/www/site-name/repo-name');
  
  $deploy-&gt;execute();
  
  ?&gt;
  </code></pre>