Php CronJob需要绝对路径
我使用CronJob以以下方式触发php脚本:Php CronJob需要绝对路径,php,path,cron,require,jobs,Php,Path,Cron,Require,Jobs,我使用CronJob以以下方式触发php脚本: /web/cgi-bin/php5 $HOME/html/library/myScript.php auth_key=xxxxxxx 但我在require上遇到了问题,我已经尝试了在stackO上能找到的一切。 似乎无法加载SendGrid自动加载器 //myScript.php #!/web/cgi-bin/php5 <?php include('../config.php'); // this works well for some r
/web/cgi-bin/php5 $HOME/html/library/myScript.php auth_key=xxxxxxx
但我在require上遇到了问题,我已经尝试了在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!
}
?>
这一点也没有:
chdir(dirname(__FILE__));
php就是followwin,我想问题就出在那里,因为这也需要调用
//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;
}
}
//SendGrid.php
您需要包括LIB
require 'scripts/sendgrid/lib/SendGrid.php';
require 'scripts/unirest/lib/Unirest.php';
我建议您使用文件的完全限定路径,否则您将需要设置include路径以使它们进入作用域
set_include_path('path/to/scripts/directory');
使用
一次使用require_的魔法常数
文件的目录。如果在包含中使用,则返回包含文件的目录。这相当于dirname(文件)。此目录名后面没有斜杠,除非它是根目录。(在PHP5.3.0中添加。)
没关系,克朗没有完成任务(没有双关语)
从现在起,我就开始使用它,它似乎做得非常完美:嗨,谢谢你的回复!我试着加入LIB,只有当我这样做的时候它才会起作用,我在我的博文中描述了你好,谢谢你的时间。我尝试了这个解决方案,但没有成功。我真的迷路了:-/真奇怪..是的,刚才。它可以工作并返回我一直使用的正确脚本,意思是:“/home/content/aa/bbbbbb/html/library”,这就是我的php脚本的播放位置。现在的问题是如何欺骗使用这些路径的SendGrid加载程序:require_once(dirname(FILE)。'/'.$FILE..php');还有$file=dir。目录分隔符$类别名称;(查看我上面的帖子)没关系,从现在开始我就在用这个:而且它似乎很管用!
set_include_path('path/to/scripts/directory');
__DIR__
require_once(realpath(__DIR__ . "/../config.php"));