Codeigniter 辅助函数返回错误
我想使用下面的代码作为辅助函数,因为它将从我的应用程序中的多个控制器调用。
正如您所看到的,这是一个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
助手是一个具有功能的文件,而不是一个类。但在助手内部,您无法像在 Codeigniter 的其余部分中那样加载模型和库。为此,您需要创建主类的一个实例,
这是您的帮助程序,functions_helper.php,位于 application/helpers/ 中:
在您的控制器或模型或视图中,您现在必须加载您的自定义帮助程序(或将其放入 application/config/autoload.php 中的自动加载器数组中
$autoload['helper'] = array('functions_helper')
;),
现在您可以按照通常的方式访问您的功能,
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/:
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')
;),
Now you can access your funcions the usual way,
你不能在函数中引用 $this (阅读 OOP)
使用 get_instance() 来获取对控制器对象的引用,如下所示:
you can't reference $this inside a function (read about OOP)
use get_instance() to grab a reference to the controller object, something like: