【问题标题】:CronJob require absolute pathCronJob 需要绝对路径
【发布时间】:2014-03-23 16:25:40
【问题描述】:

我正在使用 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 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;
}
}

当然非常感谢任何帮助! 提前致谢,Lois

【问题讨论】:

    标签: php path cron require jobs


    【解决方案1】:

    没关系,Cron 没有完成这项工作(没有双关语)。

    我从现在开始使用它,它似乎完美地完成了工作:http://atrigger.com/

    【讨论】:

      【解决方案2】:

      使用

      __DIR__     
      

      require_once 的魔法常数。

      文件的目录。如果在包含中使用,则返回包含文件的目录。这相当于 dirname(FILE)。此目录名称没有尾部斜杠,除非它是根目录。 (在 PHP 5.3.0 中添加。)

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

      【讨论】:

      • 您好,感谢您的宝贵时间。我尝试了这个解决方案,但没有奏效。我真的迷路了:-/这很奇怪..
      • 是的,刚才。它可以工作并返回我一直在使用的正确的,意思是:“/home/content/aa/bbbbbb/html/library”,这是我的 php 脚本播放的地方。现在的问题是如何欺骗在内部使用这些路径的 SendGrid 加载器: require_once(dirname(FILE) . '/' . $file . '.php');还有 $file = dir 。目录分隔符。 $类名; (查看我上面的帖子)
      • 没关系,我从现在开始使用这个:atrigger.com,它似乎工作!
      【解决方案3】:

      您需要包含库

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

      我建议您使用文件的完全限定路径 - 否则您需要设置包含路径以将它们放入范围

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

      【讨论】:

      • 您好,感谢您的回复!好吧,我尝试包含库,只有当我按照我在帖子中描述的方式这样做时它才有效
      猜你喜欢
      • 1970-01-01
      • 2014-02-09
      • 1970-01-01
      • 1970-01-01
      • 2018-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-23
      相关资源
      最近更新 更多