2011-06-14 74 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