Question

I'm using CronJob the following way to trigger a php script:

/web/cgi-bin/php5 $HOME/html/library/myScript.php auth_key=xxxxxxx

But I experience problems with require and I tried everything I could find on stackO already. It seems it can't load the SendGrid autoloader

//myScript.php
#!/web/cgi-bin/php5
<?php
include('../config.php'); // this works well for some reason...

if(isset($_GET['auth_key']) && $_GET['auth_key'] == "xxxxxxx")
{
    // send email
    include('home/content/aa/bbbbbb/html/scripts/sendgrid/lib/SendGrid.php'); // this works only this way
    include('home/content/aa/bbbbbb/html/scripts/unirest/lib/Unirest.php'); // this too

    SendGrid::register_autoloader(); // fails here!
}
?>

And this did not work either:

set_include_path('/home/content/aa/bbbbbb/html/');
require 'scripts/sendgrid/lib/SendGrid.php';
require 'scripts/unirest/lib/Unirest.php';

And neither did this:

chdir(dirname(__FILE__));

The SendGrid.php is the folowwin, and I reckon the problem lies in there somewhere since this makes also require calls!

//SendGrid.php
<?php
class SendGrid {
const VERSION = "1.1.5";

protected $namespace = "SendGrid",
        $username,
        $password,
        $web,
        $smtp;

public function __construct($username, $password) {
    $this->username = $username;
    $this->password = $password;
}

public static function register_autoloader() {
    spl_autoload_register(array('SendGrid', 'autoloader'));
}

public static function autoloader($class) {
// Check that the class starts with "SendGrid"
if ($class == 'SendGrid' || stripos($class, 'SendGrid\\') === 0) {
    $file = str_replace('\\', '/', $class);

    if (file_exists(dirname(__FILE__) . '/' . $file . '.php')) {
    require_once(dirname(__FILE__) . '/' . $file . '.php');
    }
}
}

public function __get($api) {
$name = $api;

if($this->$name != null) {
  return $this->$name;
}

$api = $this->namespace . "\\" . ucwords($api);
$class_name = str_replace('\\', '/', "$api.php");
$file = __dir__ . DIRECTORY_SEPARATOR . $class_name;

if (!file_exists($file)) {
  throw new Exception("Api '$class_name' not found.");
}
require_once $file;

$this->$name = new $api($this->username, $this->password);
return $this->$name;
}
}

Any help is much appreciated of course! Thanks in advance, Lois

Was it helpful?

Solution 3

Never mind, Cron did not fulfill the job (no pun intended).

I'm using this from now on and it seems to do the work just perfectly: http://atrigger.com/

OTHER TIPS

You need to include the the libs

require 'scripts/sendgrid/lib/SendGrid.php';
require 'scripts/unirest/lib/Unirest.php';

I suggest you use fully qualified paths to the files - otherwise you will need to set the include path to get them into scope

set_include_path('path/to/scripts/directory');

Use

__DIR__     

magic constant with require_once.

The directory of the file. If used inside an include, the directory of the included file is returned. This is equivalent to dirname(FILE). This directory name does not have a trailing slash unless it is the root directory. (Added in PHP 5.3.0.)

require_once(realpath(__DIR__ . "/../config.php"));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top