Codeigniter 辅助函数返回错误

发布于 2024-11-15 13:35:42 字数 2178 浏览 3 评论 0原文

我想使用下面的代码作为辅助函数,因为它将从我的应用程序中的多个控制器调用。

正如您所看到的,这是一个 PHP 卷曲,用于将文本发布到用户的 Facebook 墙上。

} elseif ($this->input->post('post_facebook')) { //"post to facebook" checkbox is ticked

        $this->load->model('facebook_model');

        $token = $this->facebook_model->get_facebook_cookie();

        $attachment =  array(
            'access_token'  => $token['access_token'],
            'message'       => $post['posts_text'],
        );

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,'https://graph.facebook.com/' . $token['uid'] . '/feed');
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  //to suppress the curl output
        $result = curl_exec($ch);
        curl_close($ch);

该代码在控制器内部时可以完美运行。但我想做这样的事情:

把它放在 helpers/functions_helper.php 中:

function post_facebook_wall($post) {

    $this->load->model('facebook_model');

    $token = $this->facebook_model->get_facebook_cookie();

    $attachment =  array(
        'access_token'  => $token['access_token'],
        'message'       => $post['posts_text'],
    );

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,'https://graph.facebook.com/' . $token['uid'] . '/feed');
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  //to suppress the curl output
    $result = curl_exec($ch);
    curl_close($ch);        }

然后在我的控制器中:

...
} elseif ($this->input->post('post_facebook')) { //post to facebook checkbox is ticked

    $post = array('posts_text' => 'sample text');

    post_facebook_wall($post);

}

不幸的是,这不起作用,我收到 500 错误。

functions_helper.php 处于自动加载状态。

你知道我在这里做错了什么吗?提前致谢。

I would like to use the code below as a helper function, since it will be called from several controllers in my app.

As you can see this is a PHP curl to post text to a user's Facebook wall.

} elseif ($this->input->post('post_facebook')) { //"post to facebook" checkbox is ticked

        $this->load->model('facebook_model');

        $token = $this->facebook_model->get_facebook_cookie();

        $attachment =  array(
            'access_token'  => $token['access_token'],
            'message'       => $post['posts_text'],
        );

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,'https://graph.facebook.com/' . $token['uid'] . '/feed');
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  //to suppress the curl output
        $result = curl_exec($ch);
        curl_close($ch);

This code works perfectly when inside a controller. But I would like to do something like this instead:

Put this in helpers/functions_helper.php:

function post_facebook_wall($post) {

    $this->load->model('facebook_model');

    $token = $this->facebook_model->get_facebook_cookie();

    $attachment =  array(
        'access_token'  => $token['access_token'],
        'message'       => $post['posts_text'],
    );

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,'https://graph.facebook.com/' . $token['uid'] . '/feed');
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  //to suppress the curl output
    $result = curl_exec($ch);
    curl_close($ch);        }

Then in my controller:

...
} elseif ($this->input->post('post_facebook')) { //post to facebook checkbox is ticked

    $post = array('posts_text' => 'sample text');

    post_facebook_wall($post);

}

Unfortunately this isn't working, and I get a 500 error.

functions_helper.php is in autoload.

Any ideas what I'm doing wrong here? Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

舟遥客 2024-11-22 13:35:42

助手是一个具有功能的文件,而不是一个类。但在助手内部,您无法像在 Codeigniter 的其余部分中那样加载模型和库。为此,您需要创建主类的一个实例,

这是您的帮助程序,functions_helper.php,位于 application/helpers/ 中:

   If ( ! defined('BASEPATH')) exit('No direct script access allowed');

   If ( ! function_exists('post_facebook_wall'))
   {
      function post_facebook_wall($post) {

        $CI =& get_instance();   //instantiate CodeIngiter main class
        //now you can call your models:
        $CI->load->model('facebook_model');

        $token = $CI->facebook_model->get_facebook_cookie();
        $attachment =  array(
        'access_token'  => $token['access_token'],
        'message'       => $post['posts_text'] );

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,'https://graph.facebook.com/' . $token['uid'] . '/feed');
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  //to suppress the curl output
        $result = curl_exec($ch);
        curl_close($ch);
    }

在您的控制器或模型或视图中,您现在必须加载您的自定义帮助程序(或将其放入 application/config/autoload.php 中的自动加载器数组中 $autoload['helper'] = array('functions_helper');
),

   $this->load->helper('functions_helper');

现在您可以按照通常的方式访问您的功能,

$this->functions_helper->post_facebook_wall($post)

A helper is a file with function, and not a class. But inside a helper you cannot load models and libraries as in the rest of Codeigniter. In order to do that, you need to create an instance of the main class,

This is your helper, functions_helper.php, located in application/helpers/:

   If ( ! defined('BASEPATH')) exit('No direct script access allowed');

   If ( ! function_exists('post_facebook_wall'))
   {
      function post_facebook_wall($post) {

        $CI =& get_instance();   //instantiate CodeIngiter main class
        //now you can call your models:
        $CI->load->model('facebook_model');

        $token = $CI->facebook_model->get_facebook_cookie();
        $attachment =  array(
        'access_token'  => $token['access_token'],
        'message'       => $post['posts_text'] );

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,'https://graph.facebook.com/' . $token['uid'] . '/feed');
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  //to suppress the curl output
        $result = curl_exec($ch);
        curl_close($ch);
    }

In your controllers or models or views you now have to LOAD your custom helper (or place it in the autoloader array in application/config/autoload.php $autoload['helper'] = array('functions_helper');
),

   $this->load->helper('functions_helper');

Now you can access your funcions the usual way,

$this->functions_helper->post_facebook_wall($post)
巴黎盛开的樱花 2024-11-22 13:35:42

你不能在函数中引用 $this (阅读 OOP)

使用 get_instance() 来获取对控制器对象的引用,如下所示:

$obj = get_instance();
$obj->load->model('facebook_model');
// etc

you can't reference $this inside a function (read about OOP)

use get_instance() to grab a reference to the controller object, something like:

$obj = get_instance();
$obj->load->model('facebook_model');
// etc
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文