CodeIgniter Passing POST data from RestClient to RestServer API("SyntaxError: JSON.parse: unexpected character at line 2 column 1 of the JSON data") - codeigniter

I've spent my whole day trying to figure out this problem. Posting this issue here is my last hope. I hope someone can help to continue working on my first job.
SyntaxError: JSON.parse: unexpected character at line 2 column 1 of the JSON data
So, POST works fine when directly passing data from my views to the RestServer directly. However, RESTServer API is not able to find POSTs data sent from the RestClient.
my Example
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require APPPATH . 'libraries/REST_Controller.php';
class Example extends REST_Controller {
function __construct()
{
parent::__construct();
$this->methods['get']['limit'] = 500;
$this->methods['post']['limit'] = 200;
$this->methods['delete']['limit'] = 50;
}
public function post()
{
$message = array(
'id' => null,
'name' => $this->input->post('name'),
'message' => $this->input->post('email')
);
$this->set_response($message);
}
}

First off:, try renaming your POST function from post() to index_post() that is simply a standard that can help with SOLID principles.
Second:
Is there in your Controller somewhere whare you put the use CI_Controller namespace either you added it in the REST_Controller.php file, since it is not in the Example controller; just REMOVE that namespace. The below 3 requirements should work just fine for you.
require(APPPATH . 'libraries/REST_Controller.php');
use Restserver\Libraries\REST_Controller;
require (APPPATH . 'libraries/Format.php');
class Example extends REST_Controller {
...
}

Related

Getting Error in CodeIgniter RESTful API

defined('BASEPATH') OR exit('No direct script access allowed');
require('application/libraries/REST_Controller.php');
use Restserver\Libraries\REST_Controller;
class demo extends REST_Controller {
function __construct(){
parent::__construct();
}
public function demo1_get()
{
echo 'demo 1';
}
}
Error Image
enter image description here
This is My REST_Controller
enter image description here
Hi, I m getting Error for making Codeigniter RESTful API. Please Help me to solve this problem.
It should be like this :
Make sure you have REST_Controller.php in your libraries folder
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require_once APPPATH . '/libraries/REST_Controller.php';
class Demo extends REST_Controller
{
function __construct(){
parent::__construct();
}
public function demo1_get()
{
echo 'demo 1';
}
}
You need to follow the link https://itsolutionstuff.com/post/codeigniter-3-restful-api-tutorialexample.html
and then after you run the code you will get a small error Unable to load the requested language file: language/english/rest_controller_lang.php
The problem is that codeigniter can't find the rest_controller translations. You just need to create this file /application/languages/english/rest_controller_lang.php
Then copy & paste this code inside:
<?php
/*
* English language
*/
$lang['text_rest_invalid_api_key'] = 'Invalid API key %s'; // %s is the REST API key
$lang['text_rest_invalid_credentials'] = 'Invalid credentials';
$lang['text_rest_ip_denied'] = 'IP denied';
$lang['text_rest_ip_unauthorized'] = 'IP unauthorized';
$lang['text_rest_unauthorized'] = 'Unauthorized';
$lang['text_rest_ajax_only'] = 'Only AJAX requests are allowed';
$lang['text_rest_api_key_unauthorized'] = 'This API key does not have access to the requested controller';
$lang['text_rest_api_key_permissions'] = 'This API key does not have enough permissions';
$lang['text_rest_api_key_time_limit'] = 'This API key has reached the time limit for this method';
$lang['text_rest_ip_address_time_limit'] = 'This IP Address has reached the time limit for this method';
$lang['text_rest_unknown_method'] = 'Unknown method';
$lang['text_rest_unsupported'] = 'Unsupported protocol';
Hope this helps

how to get a passed data from the url in codeigniter

Hi i have this data passed in the url
http://www.test.com/dashboard/john-doe
i want to get the page when i enter http://www.test.com/dashboard/john-doe
john doe is a name from the database i retrieve when the users can login.
so dashboard is a controller then how will i able to get the page if i passed a data john-doe next to dashboard controller? can someone help me figured this thing out? Any help is muchly appreicated this is my controller below
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Dashboard extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->model('users_model', 'um');
$sessionData = $this->session->userdata('loggedIn');
$this->data['id'] = $sessionData['userID'];
if(!$this->session->userdata('loggedIn')){
redirect('login');
}
}
public function index($slug){
echo $slug; exit;
$this->data['title'] = 'Dashboard | test';
$this->data['menu'] = 'dashboard';
//get CustomerInfo
$this->data['getCustomerInfo'] = $this->um->getCustomerInfo($this->data['id']);
$this->template_lib->set_view('templateLogIn', 'dashboard', $this->data,'',$this->data);
}
}
The index function is loaded by default if the second segment of the URI is empty. If you have parameters you need to define a route:
$route['dashboard/(:any)'] = "dashboard/index/$1";

Codeigniter extend Blueimp Upload library

I have blueimp file upload working well with codeigniter. I use the UploadHandler library as is. But I want to extend it to replace two functions that create the unique filename. The code for this is in the BlueImp Github wiki.
I created an extended library thus:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_UploadHandler extends UploadHandler {
public function __construct() {
parent::__construct();
$CI =& get_instance();
$CI->load->library('UploadHandler');
}
protected function upcount_name_callback($matches) {
$index = isset($matches[1]) ? intval($matches[1]) + 1 : 1;
$ext = isset($matches[2]) ? $matches[2] : '';
return '-'.$index.''.$ext;
}
protected function upcount_name($name) {
return preg_replace_callback(
'/(?:(?:-([\d]+))?(\.[^.]+))?$/',
array($this, 'upcount_name_callback'),
$name,
1
);
}
}
When I try to run it, I get an 'unable to load UploadHandler' error. If I remove my MY extension, the original code runs. What is wrong with my extension code? Isn't this the proper way to extend CI libraries?
And, yes, the filename for my file is MY_UploadHandler.php
Thanks!
I noodled over this for a while and finally figured it out. To extend a custom library, you need to 'require once' the file it is extending, prior to the definition of the class.
EX:
require_once("UploadHandler.php");
class MY_UploadHandler extends UploadHandler
{
}
Hopefully that helps.

Codeigniter XML-RPC Sample code issue

Trying to run Codeigniter User Guide XML RPC Sample Code.
This is the code
xmlrpc_client.php
<?php
class Xmlrpc_client extends CI_Controller {
function index()
{
$this->load->helper('url');
$server_url = site_url('xmlrpc_server');
$this->load->library('xmlrpc');
$this->xmlrpc->server($server_url, 80);
$this->xmlrpc->method('Greetings');
$request = array('How is it going?');
$this->xmlrpc->request($request);
if ( ! $this->xmlrpc->send_request())
{
echo $this->xmlrpc->display_error();
}
else
{
echo '<pre>';
print_r($this->xmlrpc->display_response());
echo '</pre>';
}
}}?>
xmlrpc_server.php
<?php
class Xmlrpc_server extends CI_Controller {
function index()
{
$this->load->library('xmlrpc');
$this->load->library('xmlrpcs');
$config['functions']['Greetings'] = array('function' => 'Xmlrpc_server.process');
$this->xmlrpcs->initialize($config);
$this->xmlrpcs->serve();
}
function process($request)
{
$parameters = $request->output_parameters();
$response = array(
array(
'you_said' => $parameters['0'],
'i_respond' => 'Not bad at all.'),
'struct');
return $this->xmlrpc->send_response($response);
}}?>
After this, i ran the url like this.
remoteserver's ip/xmlrpc_client
(i deleted my index.php using .htaccess, dont need to type it)
the result is like this,
Did not receive a '200 OK' response from remote server. (HTTP/1.1 404 Not Found)
If i run the server code,
remoteserver's ip/xmlrpc_server
it says like this.
This XML file does not appear to have any style information associated with it. The document tree is shown below.
Which means,
$this->xmlrpc->send_request()
this request have been failed
and echoed
echo $this->xmlrpc->display_error();
Any idea what is the problem is?
Oh, i have another question.
Do i have to install xmlrpc php extension before i use this codeigniter xmlrpc class?
Solved! the problem was the remote sever's firewall.

Codeigniter MVC Sample Code

Hi
I am following the getting started guide for Codeigniterr given at http://www.ibm.com/developerworks/web/library/wa-codeigniter/
I have followed the instruction to create the front view and added controller to handle form submission. Ideally, when i submit the form, it should load the model class and execute the function to put details on the database, but instead it is just printing out the code of the model in the browser.
**Code of view (Welcome.php)**
----------------
<?php
class Welcome extends Controller {
function Welcome()
{
parent::Controller();
}
function index()
{
$this->load->helper('form');
$data['title'] = "Welcome to our Site";
$data['headline'] = "Welcome!";
$data['include'] = 'home';
$this->load->vars($data);
$this->load->view('template');
}
function contactus(){
$this->load->helper('url');
$this->load->model('mcontacts','',TRUE);
$this->mcontacts->addContact();
redirect('welcome/thankyou','refresh');
}
function thankyou(){
$data['title'] = "Thank You!";
$data['headline'] = "Thanks!";
$data['include'] = 'thanks';
$this->load->vars($data);
$this->load->view('template');
}
}
/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */
**Code of Model**
--------------
class mcontacts extends Model{
function mcontacts(){
parent::Model();
}
}
function addContact(){
$now = date("Y-m-d H:i:s");
$data = array(
'name' => $this->input->xss_clean($this->input->post('name')),
'email' => $this->input->xss_clean($this->input->post('email')),
'notes' => $this->input->xss_clean($this->input->post('notes')),
'ipaddress' => $this->input->ip_address(),
'stamp' => $now
);
$this->db->insert('contacts', $data);
}
**OUTPUT after clicking submit**
-----------------------------
class mcontacts extends Model{ function mcontacts(){ parent::Model(); } } function addContact(){ $now = date("Y-m-d H:i:s"); $data = array( 'name' => $this->input->xss_clean($this->input->post('name')), 'email' => $this->input->xss_clean($this->input->post('email')), 'notes' => $this->input->xss_clean($this->input->post('notes')), 'ipaddress' => $this->input->ip_address(), 'stamp' => $now ); $this->db->insert('contacts', $data); }
I have tried doing these things
1. Making all PHP codes executable
2. Change ownership of files to www-data
3. make permission 777 for whole of www
But, the code of model seems to be just printed ... PLEASE HELP
Just a few minor points that might help you:
In your controller, point the index method at the method you would like to call on that page. For example:
function index()
{
$this->welcome();
}
That will help keep things clean and clear, especially if anyone else comes in to work on the code with you later. I chose welcome because that's the name of your controller class and that will keep things simple.
In your model, this:
class mcontacts extends Model{
Should be:
class Mcontacts extends Model{
Capitalize those class names! That could be giving you the trouble you describe.
See here for more info on this: http://codeigniter.com/user_guide/general/models.html
Don't use camel case in your class or method names. This isn't something that will cause your code to fail, but it's generally accepted practice. See Codeigniter's PHP Style guide for more information on this: http://codeigniter.com/user_guide/general/styleguide.html
It's difficult to see with the formatting as it is, but do have an extra curly brace after the constructor method (mcontacts()) in the model? This would cause problems! Also although the code looks generally ok, there are probably better ways to use the framework especially if you do anything more complicated than what you've shown. For example, autoloading, form validation etc. Can I suggest you have a read of the userguide? It's very thorough and clear and should help you alot. http://codeigniter.com/user_guide/index.html

Resources