【问题标题】:Codeigniter Helper function returning errorCodeigniter Helper 函数返回错误
【发布时间】:2011-06-14 18:56:17
【问题描述】:

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

如您所见,这是一个用于将文本发布到用户 Facebook 墙上的 PHP curl。

} 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 正在自动加载中。

有什么想法我在这里做错了吗?提前致谢。

【问题讨论】:

    标签: php codeigniter helper


    【解决方案1】:

    助手是一个有函数的文件,而不是一个类。但是在助手中,您不能像在 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)
    

    【讨论】:

      【解决方案2】:

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

      使用 get_instance() 获取对控制器对象的引用,例如:

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

      【讨论】:

        猜你喜欢
        • 2016-04-17
        • 2014-06-26
        • 2011-10-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多