2011-06-14 56 views
0

我想使用下面的代码作为帮助函数,因为它会从我的应用程序中的几个控制器调用。Codeigniter帮助函数返回错误

正如你所看到的,这是一个PHP curl文本发布到用户的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); 

这段代码在控制器内部工作时是完美的。但我想这样做,而不是:

将这个在助手/ 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在自动载入。

任何想法我在做什么错在这里?提前致谢。

+0

什么在你的error.log? – afarazit 2011-06-14 20:10:22

回答

2

助手就是一个文件与功能,而不是一个类。但是在助手内部,不能像在Codeigniter的其余部分那样加载模型和库。为了做到这一点,你需要创建一个主类的实例,

这是你的帮手,functions_helper.php,位于应用程序/佣工/:

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

在您的控制器或模型或意见你现在必须装入自定义助手(或把它放在应用程序自动加载磁带机阵列/配置/ autoload.php $autoload['helper'] = array('functions_helper')中; ),

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

现在,您可以访问您的funcions通常的方式,

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

非常好的答案+代码完美的工作 - 非常感谢 – pepe 2011-06-14 20:52:52

+0

@torr很高兴有帮助;) – 2011-06-14 21:05:55

1

不能引用这个$里面的函数(了解OOP)

使用get_instance()抢控制器对象的引用,是这样的:

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

甚至没有+1? T_T ..顺便说一句,使用=&被弃用 – HappyDeveloper 2011-06-15 12:03:45

+0

oops - 忘了点击箭头..:P – pepe 2011-06-15 13:25:18