Ok folks,
I have an odd issue with a function of mine.
public function getOutages($Site)
{
// pull a json data dump of all outages
If(!$Site){
echo '[{}]';
}else{
$this->load->database('default', TRUE);
$this->db->where('Clear', '0');
$this->db->where('FracID', $Site);
$query = $this->db->get('vw_Outages');
echo json_encode($query->result_array());
}
}
This when accesed directly will not echo anything. By enabling the profiler though it functions fine and outputs the data.
public function getOutages($Site)
{
$this->output->enable_profiler(TRUE);
// pull a json data dump of all outages
If(!$Site){
echo '[{}]';
}else{
$this->load->database('default', TRUE);
$this->db->where('Clear', '0');
$this->db->where('FracID', $Site);
$query = $this->db->get('vw_Outages');
echo json_encode($query->result_array());
}
}
Any insight into this would be more then welcome :D .
CodeIgniter has an output buffering system (which also allows it to do things like cache controller output, set headers, and collect view output). You don't usually echo from a controller method. Do this instead:
public function mymethod() {
$anobject = array();
$output = json_encode($anobject);
$this->output->set_content_type('application/json');
$this->output->set_output($output);
}
See the CodeIgniter documentation for the Output class.
Maybe you have output buffering or compression enabled-this could cause problems like this. Also check that the variable you're trying to output isn't empty. If this doesn't help, try using a view to display data.
Related
I'm using PHP 5.3, and trying to develop a simple web service that gets some parameters with POST method and has a response.
function start(){
getAndValidateParams();
global $response;
echo json_encode($response);
}
function getAndValidateParams(){
// token (mandatory)
if(isset($_POST[PARAM_TOKEN])){
echo 'got your token';
}else{
$response[ERROR_CODE] = ERR2_INVALID_TOKEN;
$response[DESCRIPTION] = CODE2_DESC;
}
}
I'm trying to test that with Postman:
The problems:
1. About the Xdebug HTML I saw the following question, If I turn the var_dump off, will it disable usage of var_dump() inside my php code? (I want to be able use it for debugging but not seeing that in the response).
2.Also I have a problem to pass the parameter 'token', I don't see it in getAndValidateParams().
Any help will be appreciated.
I have used your function to just get insight in this and for tesing you can use also there is Advanced REST client in chrome similar to postMAN that you are using --
use the below lines to debug this --
function start(){
$response = getAndValidateParams();
return json_encode($response);
}
// calling function ends here
// statrt another function that is being called
function getAndValidateParams(){
// token (mandatory)
// print_r($_POST);die; // just for debug purpose
if(isset($_POST[PARAM_TOKEN])){
$response[ERROR_CODE] = 0;
$response[DESCRIPTION] = "Success";
$response[DEtail] = $yourdetailarr; // array of data that you want to retuen
}else{
$response[ERROR_CODE] = ERR2_INVALID_TOKEN;
$response[DESCRIPTION] = CODE2_DESC;
}
return $response;
}
/// ends here
check the response here by calling start function .
I am using xmlprc server in codeignter for web services . the flow of my application is that i need to pass parameters to the xmlrpc server method which then should invoke another controller class method which would set the parameters in a js function and that js method is invoked concurrently .
The problem i am facing is in calling the controller class method from the xmlrpc server method and getting the response to the server parent method which could then be fetched using xmlhttprequest.
my xmlrpc server method is:
function update_p($request) {
$parameters = $request->output_parameters();
$this->session->set_userdata(array("portfolio" =>$parameters['0']["portfolio"]));
$this->session->set_userdata(array("filter" =>$parameters['0']["filter"]));
$url = base_url("ControllerClass/update_p?".$parameters['0']["portfolio"].'&'.$parameters['0']["filter"]);
header("Location: $url");
$xml_rpc_rows=array("portfolio"=>$parameters['0']["portfolio"],"filter"=>$parameters['0']["filter"]);
$response = array(
$xml_rpc_rows,
'struct');
$this->xmlrpc->send_response($response);
}
Controller Class method:
public function update_p() {
$loginid = $this->session->userdata('loginid');
if(!isset($loginid)){
die;
}
error_reporting(E_ERROR);
if (time()>$this->session->userdata('expire')) { redirect("/dashboard/logout?expired=Y","location",401); die; }
$out='';
$request="USER ".$loginid.($this->session->userdata('isMobile')?"#mobile":"")."\n";
if(isset($_GET["portfolio"])) {
$portfolio=trim($_GET["portfolio"]);
$request.='ECHO "LISTP":'."\nLISTP0 #".$portfolio;
if(isset($_GET["filter"])) {
$filter=trim($_GET["filter"]);
$request.=" -".$filter;
}
if(isset($_GET["sort"])) {
$sort=trim($_GET["sort"]);
if ($sort>=1024) $request.=" -s".($sort&1023);
else $request.=" -S".$sort;
}
$ph = isset($_GET["first"]);
if ($ph) {
$this->load->model('Model');
$resultArray = $this->Model->getData($this->session->userdata('loginid'),$this->session->userdata('isMobile')?'mobile':'default','listp');
$request.=" ".$resultArray[0]['listp'];
}
$request.="\nECHO ,\n";
if(isset($_GET["watch"])) {
$portfolio=trim($_GET["watch"]);
if ($ph)
$resultArray = $this->Model->getData($this->session->userdata('loginid'),$this->session->userdata('isMobile')?'mobile':'default','watch');
$request.='ECHO "watchl":'."\nLISTP1 #".$portfolio." -WL ".($ph?$resultArray[0]['watch']:"")."\n";
$request.='ECHO ,"watchs":'."\nLISTP1 #".$portfolio." -WS\nECHO ,\n";
}
}
$request.="RISk\nECHO ,\nPnL\n";
if ($result=$this->getData($request."BYE\n")) {
if (result!='') $out=$result."\n";
}
ob_start('ob_gzhandler');
echo "{".$out."}";
ob_end_flush();
}
I can not figure out how to get the controller method result in the server method anyone who can shed some light on this would be much appreciated .
Thankyou.
Your controller method is expecting to output the result as an echo statement, which goes to the browser, rather than to return it in a variable. This means your server function is having to try to capture the output of that controller method. That setup is much more awkward and prone to error.
Unless you also need to access your update_p method directly from a browser you should change your Controller to simply return the output which really means this controller is more of a library and should probably go in the libraries folder. You will need to change your controller code a bit so that instead of grabbing the parameters from $_GET you are getting them as arguments, which in CodeIgniter is what you should be doing anyway.
So from the end of update_p just do this instead of your echo:
return "{".$out."}";
Then in your xmlrpc server do this:
$controller = new ControllerClass();
$result = $controller->update_p($parameters['0']["portfolio"], $parameters['0']["filter"]);
Then do whatever you want with your $result.
We are using CodeIgniter to wrap some internal functions and to display the results in json.
Occasionally our internal functions might have debug strings printed to the screen. So we have started using output buffering to capture any debug and add this to the result $data['response']['debug'] = $ob;
This seems to have worked pretty well as we have started dropping this code into most our publicly exposed CI functions.
We seem to be repeating this code flow a-lot now.
What would be the best way to extract the 'repeated' sections into a general template that is called when required? (not every function implements this - just most).
Example code:
public function some_function($var){
ob_start(); //repeated
$this->load->model('some_model');
$result = $this->some_model->do_something($var);
if($result){
$data['response']['result'] = true;
}else{
$data['response']['error'] = 'Something not found.';
}
$ob = ob_get_clean(); //repeated
if($ob!=''){
$data['response']['debug'] = $ob; //repeated
}
$this->load->view('json',$data); //repeated
}
If you have PHP 5.3.0 or higher, you can do something like this :
function debug_func($code){
ob_start(); //repeated
$code();
$ob = ob_get_clean(); //repeated
if($ob!=''){
$data['response']['debug'] = $ob; //repeated
}
$this->load->view('json',$data); //repeated
}
and use your code like this :
debug_func(function(){
$this->load->model('some_model');
$result = $this->some_model->do_something($var);
if($result){
$data['response']['result'] = true;
}else{
$data['response']['error'] = 'Something not found.';
}
});
and do all your codeing like that.
Ofcourse you can also wrap the repeated code in 2 functions and call them instead, just as annoying but will save you some space.
i am having strange issue with yii framework. on localhost, ajax response takes 200ms (which is fast and i am satsified) where as on my live server, same function take 4 to 7 seconds.
below is my php ajax function:-
public function actionOpenpopup() {
$this->checkAjaxRequest();
$user_id = $_GET['uid'];
$rows = Yii::app()->db->createCommand()
->select('*')
->from('saved_designs')
->where('uid=:id', array(':id' => $user_id))
->order('date desc')
->queryAll();
$i = 0;
foreach ($rows as $row) {
$rows[$i] = $row;
$i++;
}
if ($rows) {
echo json_encode($rows);
}
else
echo json_encode(null);
}
function checkAjaxRequest() {
if (Yii::app()->request->isAjaxRequest) {
header('Content-Type: application/json; charset="UTF-8"');
return true;
} else {
throw new CHttpException('403', 'Forbidden Access');
exit;
}
}
javascript code is:-
function sendAjaxCall(data){
$.ajax({
type : 'GET',
url : 'index.php/request/openpopup',
datatype : 'json',
data :data,
success: function (data) {
console.log(data);
}
});
}
*Note:- So far database has only 10 to 20 records. Also On live server, all my ajax calls give me slow response.
I would try a few things. First off after you echo your json I would kill your script to make sure nothing else runs:
if ($rows) {
echo json_encode($rows);
die();
}
Also on your index.php make sure you have the site taken out of debug mode, if you have either of the middle two lines that start with defined() enabled each page load Yii is recreating cached files and it can take a while, especially if you have extensions like bootstrap included. I have had this exact issue when doing some work for someone and their site was hosted on GoDaddy. For some reason the file creation was really slow and was really dragging everything.
<?php
$yii=dirname(__FILE__).'/../framework/yii.php';
$config=dirname(__FILE__).'/protected/config/test.php';
//defined('YII_DEBUG') or define('YII_DEBUG',true);
//defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',3);
require_once($yii);
Yii::createWebApplication($config)->run();
Also are any other functions running slow? Any errors in your error log?
Another option to help debug create another action that doesn't require a AJAX call. It is much easier to debug this way instead of relying on ajax, plus it helps you narrow down the source of the problem. Plus don't know why but you get your array of rows then re-populate your array of rows, this is very redundant.
public function actionCheckpopup() {
$user_id = $_GET['uid'];
$rows = Yii::app()->db->createCommand()
->select('*')
->from('saved_designs')
->where('uid=:id', array(':id' => $user_id))
->order('date desc')
->queryAll();
echo json_encode($rows);
die();
}
Then simply use a browser and go to http://yoursite.com/index.php/request/checkpopup?uid=1
Im trying to use the GiantBomb api to query video games, and currently when I enter the URL into a browser, it works just fine. The Json data shows up.
Heres an example url..
http://www.giantbomb.com/api/search/?api_key=83611ac10d0dfghfgh157177ecb92b0a5a2350c59a5de4&query=Mortal+Kombat&format=json
But when I try to use my php wrapper that Im just starting to build, it returns html??
Heres the start of my wrapper code....(very amateur for now)
You'll notice in the 'request' method, Ive commented out the return for json_decode($url), because when I uncomment it, the page throws a 500 error??? So I wanted to see what happends when I just echo it. And it echos an html page. Surely it should just echo what is shown, when you just enter that url into the browser, no?
However...if I replace the url with say a GoogleMap url, it echoes out Json data just fine, without using json_decode. Any ideas as to wahts going on here????
class GiantBombApi {
public $api_key;
public $base_url;
public $format;
function __construct() {
$this->format="&format=json";
$this->api_key = "83611ac10d0d157177ecb92b0a5a2350c59a5de4";
$this->search_url = "http://www.giantbomb.com/api/search/?api_key=".$this- >api_key."&query=";
}
public function search($query){
$query = urlencode($query);
$url = $this->search_url.$query.$this->format;
return $this->request($url);
}
public function request($url) {
$response = file_get_contents($url);
echo $response;
//return json_decode($response, true);
}
}
//TESTING SECTION
$games = new GiantBombApi;
$query = $_GET['search'];
echo $games->search($query);
I ran a few requests through Postman and it seems that the api looks at the mime-type as well as the query string. So try setting a header of "format" to "json".