2014-02-21 84 views
0

我使用的cronjob以下方式來觸發一個PHP腳本:的cronjob需要絕對路徑

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

但我遇到問題需要和我都試過了我能找到的stackO了。 似乎無法加載SendGrid自動加載

//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! 
} 
?> 

而且這也沒有工作,要麼:

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

我也沒有這樣的:

chdir(dirname(__FILE__)); 

的SendGrid.php是folowwin,我認爲問題出在某個地方,因爲這也需要調用!

//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; 
} 
} 

當然,任何幫助是非常感謝! 由於提前,洛伊絲

回答

0

沒關係,克朗沒有完成這項工作(沒有雙關語意圖)。

我使用這個從現在開始,它似乎做的工作只是完美:http://atrigger.com/

0

您需要包括在庫

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

我建議你使用完全合格的路徑的文件 - 否則,你將需要設置包括路徑,讓他們進入範圍

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

大家好,感謝您的回覆!那麼我試圖包括這些庫,它只有當我這樣做時才起作用我在我的帖子中描述 – Lois

0

使用

__DIR__  

魔術常量require_o NCE。

該文件的目錄。如果在include中使用,則返回包含文件的目錄。這相當於dirname(FILE)。除非目錄名是根目錄,否則該目錄名沒有結尾斜槓。 (在PHP 5.3.0中添加)。

require_once(realpath(__DIR__ . "/../config.php")); 
+0

你好,感謝你的時間。我試過這個解決方案,但沒有工作。我真的迷路了: - /很奇怪.. – Lois

+0

你試過var_dump realpath的結果嗎? – enterx

+0

是的,就在現在。它的工作原理和返回我正在使用的正確的一個,這意味着:「/ home/content/aa/bbbbbb/html/library」,這是我的PHP腳本播放的地方。現在的問題是如何欺騙內部使用這些路徑的SendGrid加載器:require_once(dirname(__ FILE__)。'/'。$ file。'.php');還有$ file = __dir__。 DIRECTORY_SEPARATOR。 $ CLASS_NAME; (檢查我的帖子上面) – Lois