我是Codeigniter的新手,我正在开发API类,以下是API代码
<?php
require_once("WiZiQService.php");
class ScheduleClass{
function ScheduleClass($secretAcessKey,$access_key,$webServiceUrl)
{
require_once("AuthBase.php");
$authBase = new AuthBase($secretAcessKey,$access_key);
$method = "create";
$requestParameters["signature"]=$authBase->GenerateSignature($method,$requestParameters);
#for teacher account pass parameter 'presenter_email'
//This is the unique email of the presenter that will identify the presenter in WizIQ. Make sure to add
//this presenter email to your organization’s teacher account. ’ For more information visit at: (http://developer.wiziq.com/faqs)
$requestParameters["presenter_email"]=$By_PresenterEmail;//"kerrygun@gmail.com";
#for room based account pass parameters 'presenter_id', 'presenter_name'
//$requestParameters["presenter_id"] = "40";
//$requestParameters["presenter_name"] = "vinugeorge";
$requestParameters["start_time"] = $By_StartTime;//"2011-12-25 11:55";
$requestParameters["title"]=$By_ClassTitle;//"my php-class"; //Required
$requestParameters["duration"]=$By_ClassDuration; //optional
$requestParameters["time_zone"]=$By_TimeZone; //optional
$requestParameters["attendee_limit"]=$By_AttendeeLimit; //optional
$requestParameters["control_category_id"]=$By_ControlCategory; //optional
$requestParameters["create_recording"]=$By_CreateRecording; //optional
$requestParameters["return_url"]=$By_ReturnUrl; //optional
$requestParameters["status_ping_url"]=$By_StatusPingUrl; //optional
$requestParameters["language_culture_name"]=$By_LanguageCulture;
$httpRequest=new HttpRequest();
try
{
$XMLReturn=$httpRequest->wiziq_do_post_request($webServiceUrl.'?method=create',http_build_query($requestParameters, '', '&'));
}
catch(Exception $e)
{
echo $e->getMessage();
}
if(!empty($XMLReturn))
{
try
{
$objDOM = new DOMDocument();
$objDOM->loadXML($XMLReturn);
}
catch(Exception $e)
{
echo $e->getMessage();
}
$status=$objDOM->getElementsByTagName("rsp")->item(0);
$attribNode = $status->getAttribute("status");
if($attribNode=="ok")
{
$methodTag=$objDOM->getElementsByTagName("method");
echo "method=".$method=$methodTag->item(0)->nodeValue;
$class_idTag=$objDOM->getElementsByTagName("class_id");
echo "<br>Class ID=".$class_id=$class_idTag->item(0)->nodeValue;
$recording_urlTag=$objDOM->getElementsByTagName("recording_url");
echo "<br>recording_url=".$recording_url=$recording_urlTag->item(0)->nodeValue;
$presenter_emailTag=$objDOM->getElementsByTagName("presenter_email");
echo "<br>presenter_email=".$presenter_email=$presenter_emailTag->item(0)->nodeValue;
$presenter_urlTag=$objDOM->getElementsByTagName("presenter_url");
echo "<br>presenter_url=".$presenter_url=$presenter_urlTag->item(0)->nodeValue;
}
else if($attribNode=="fail")
{
$error=$objDOM->getElementsByTagName("error")->item(0);
echo "<br>errorcode=".$errorcode = $error->getAttribute("code");
echo "<br>errormsg=".$errormsg = $error->getAttribute("msg");
}
}//end if
}//end function
}
?>
所以我的问题是,如何将值从控制器传递到附加的外部类,输出应该返回到控制器,然后将其发送到视图。
我试图像这样使用它:
class Form extends CI_Controller {
public function index() {
$this->load->library('Create');
$this->load->view('form_view');
}
public function wizprocess() {
$this->load->library('Create');
$this->Create->ScheduleClass();
}
}
您将对以上问题的解决方案表示赞赏
答案 0 :(得分:1)
基本上有两种方法,Codeigniter方式:
$this->load->library('scheduleclass');
$this->scheduleclass->__construct($secretAcessKey, $access_key, $webServiceUrl);
我真的不喜欢这个,CI假设你的“库”构造函数有一个参数:一个配置数组。如果你改变你的类来配合它,你可以像这样加载它:
$config = array(
'secretAcessKey' => $secretAcessKey,
'access_key' => $access_key,
'webServiceUrl' => $webServiceUrl
);
$this->load->library('scheduleclass', $config);
然后你的班级可以extract($config)
或其他什么来确保定义正确的变量。
我的建议是直接使用PHP:
// Either include the file somewhere or use an autoloader
include APPPATH.'third_party/ScheduleClass.php';
然后就像往常一样,除了你没有使用CI的加载器,只是用普通的PHP创建新的对象。
// In your Controller
function mymethod()
{
$class = new ScheduleClass($secretAcessKey, $access_key, $webServiceUrl);
$this->load->view('myview', array(
'ScheduleClass' => $class
));
}
一个很大的问题是你的类的设计很糟糕,它只有一个方法(构造函数)而且直接echo
是一切而不是return
一个值,所以在你创建它的那一刻 - 所有输出都将发送到浏览器。