Can anyone show me how to insert data to mysql by using Phil Sturgeon Restful sever?
It's OK when I get data using GET method, but when I tried insert using POST method to insert data it allways echo like this:
{"status":false,"error":"Unknown method."}
<?php defined('BASEPATH') OR exit('No direct script access allowed');
require APPPATH.'/libraries/REST_Controller.php';
class Api extends REST_Controller
{
public function __construct()
{
parent::__construct();
$this->load->Model("user_model");
}
function register_post() {
$username = $this->post('username');
$password = $this->post('password');
$firstname = $this->post('firstname');
$lastname = $this->post('lastname');
$mobile = $this->post('mobile');
$sex = $this->post('sex');
$avatar = $this->post('avatar');
$active = $this->post('active');
$device_token = $this->post('device_token');
$fbconnected = $this->post('fbconnected');
$token = $this->post('token');
$register = array(
'username' => $username,
'password' => MD5('".$password."'),
'firstname' => $firstname,
'lastname' => $lastname,
'mobile' => $mobile,
'sex' => $sex,
'avatar' => $avatar,
'active' => $active,
'device_token' => $device_token,
'fbconnected' => $fbconnected,
'token' => $token
);
$this->user_model->register($register);
//$this->response($register, 200); // 200 being the HTTP response code
}
}
?>
Related
I am facing issue when i try to broadcast my video in
When I use 'startVideo' its working fine for me.
But my 'connectVideo' giving me issue, I have pasted code below. I have attached error screenshot also.
In my first function 'startVideo' I wrote code to start video its taking my webcam video, through second function 'connectVideo' i just want share/broadcast my video with my user.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
use OpenTok\OpenTok;
use OpenTok\MediaMode;
use OpenTok\ArchiveMode;
use OpenTok\Role;
use OpenTok\Session;
use OpenTok\Broadcast;
use OpenTok\Layout;
class Welcome extends CI_Controller {
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->view('welcome_message');
}
public function startVideo()
{
$opentok = new OpenTok($this->config->item('opentok_key'), $this->config->item('opentok_secret'));
$sessionOptions = array(
'archiveMode' => ArchiveMode::ALWAYS,
'mediaMode' => MediaMode::ROUTED
);
$session = $opentok->createSession($sessionOptions);
$iSessionId = $session->getSessionId();
date_default_timezone_set('Asia/Kolkata');
$created_date = date("Y-m-d H:i:s");
$data = array(
'apiKey' => $this->config->item('opentok_key'),
'sessionId' => $iSessionId,
'token' => $session->generateToken(),
'created_date' => $created_date
);
$archiveOptions = array(
'name' => 'Important Presentation', // default: null
'hasAudio' => true, // default: true
'hasVideo' => true, // default: true
// 'outputMode' => OutputMode::COMPOSED, // default: OutputMode::COMPOSED
'resolution' => '1280x720' // default: '640x480'
);
$this->load->view('video_11', $data);
}
public function connectVideo() // connect to a session
{
$opentok = new OpenTok($this->config->item('opentok_key'), $this->config->item('opentok_secret'));
$session = $opentok->createSession();
$sessionOptions = array(
'archiveMode' => ArchiveMode::ALWAYS,
'mediaMode' => MediaMode::ROUTED
);
$session = $opentok->createSession($sessionOptions);
$sessionId = $session->getSessionId();
$options = array(
'layout' => Layout::getBestFit(),
'maxDuration' => 5400,
'resolution' => '1280x720'
);
$broadcast = $opentok->startBroadcast($sessionId, $options);
// Store the broadcast ID in the database for later use
$broadcastId = $broadcast->id;
$token = 'T1==T1==cGFydG5lcl9pZD00NjQ5MTcwMiZzaWc9Y2MxYzkxYzNmYTkzNmNiNmQ0NDZiNWEzNzVhYjExYTliYTZkOTdlYzpzZXNzaW9uX2lkPTFfTVg0ME5qUTVNVGN3TW41LU1UWXhOVE00TXpJMU16WXdPWDVIWjB0WVp6SlJWRlJhVUV4c2FrczVOa0ZOZGk5SUwwOS1RWDQmY3JlYXRlX3RpbWU9MTYxNTM4MzI1MyZyb2xlPXB1Ymxpc2hlciZub25jZT0xNjE1MzgzMjUzLjY5ODUxOTY3MzAxNjU4';
$data = array(
'apiKey' => $this->config->item('opentok_key'),
'sessionId' => $session,
'token' => $token
);
$this->load->view('video_22', $data);
}
}
[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/Q1qBY.png
I am having problems when trying the API that I made. When I tested it in Postman, it appeared false, even though my username and password were correct. Can you identify the problem with my code?
I really appreciate your help.
Auth_admin.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require APPPATH . '/libraries/REST_Controller.php';
class Auth_admin extends REST_Controller
{
public function signin_post()
{
$this->load->model('model_admin', 'admin');
$params = array(
'username' => $this->post('username'),
'password' => md5($this->post('password'))
);
$result = $this->admin->admin_check($params);
if ($result){
if ($result->level == "admin"){
$response = array(
"status" => true,
"message" => "Authentication seccessfully",
"auth" => array(
"username" => $result->username
)
);
$this->set_response($response, REST_Controller::HTTP_OK);
}else{
$response = array(
"status" => false,
"message" => "This features does'nt exist for your Account"
);
$this->set_response($response, REST_Controller::HTTP_OK);
}
}
}
}
Model_admin.php
<?php
class Model_admin extends CI_Model
{
var $tablename;
public function __construct()
{
parent::__construct();
$this->tablename = "m_admin";
}
public function admin_check($data = array())
{
$params = array(
'username' => $data['username'],
'password' => $data['password'],
);
$this->db->where($params);
$query = $this->db->get($this->tablename);
return $query->row();
}
}
You have to pass function name after the controller name.
Like your path: http://192.168.122.1/project_name/auth_admin/signin it will work.
If that does not work, then please add index.php and check it because it depends on how you setup the project.
hello Everyone i am new in codeigniter i was try to insert data in database but not work properly.but not insert data in database.I was activate helper and auto load is i was write.
my controller
public function save() {
if($this->input->post('submit')) {
$this->Checkout_model->process();
}
}
my model
function process() {
$name = $this->input->post('name');
$phone = $this->input->post('phone');
$email = $this->input->post('email');
$address = $this->input->post('address');
$data=array (
'name' => $name,
'phone' => $phone,
'email' => $email,
'address' => $address
);
$this->db->insert('customers',$data);
}
Use this code In Controller
public function save() {
if($this->input->post('submit')) {
$name = $this->input->post('name');
$phone = $this->input->post('phone');
$email = $this->input->post('email');
$address = $this->input->post('address');
$data=array (
'name' => $name,
'phone' => $phone,
'email' => $email,
'address' => $address
);
$this->Checkout_model->process($data);
}
}
In Model
function process($data) {
$this->db->insert('customers',$data);
}
I have a problem in this part of my code, where i want to insert it my model named user_model with a function of checkUser2. How can i insert this data from my database, i have no error but i cannot var_dump the data that i want to insert into my database.
public function facebook_request(){
$this->load->library('facebook/src/facebook', array('appId' => '1027885817316593', 'secret' => '6db175699897b514bf4881955b2944bb'));
$this->user = $this->facebook->getUser();
if ($this->user) {
$data['user_profile'] = $this->facebook->api('/me?fields=id,first_name,last_name,email,link,gender,locale,picture');
// Get logout url of facebook
$data['logout_url'] = $this->facebook->getLogoutUrl(array('next' => base_url() . 'index.php/oauth_login/logout'));
$data = array(
'oauth_provider'=> 'facebook',
'oauth_uid' => $data['user_profile']['id'],
'first_name' => $data['user_profile']['first_name'],
'last_name' => $data['user_profile']['last_name'],
'email' => $data['user_profile']['email'],
'gender' => $data['user_profile']['gender'],
'locale' => $data['user_profile']['locale'],
'picture' => $data['user_profile']['picture']['data']['url'],
'link' => $data['user_profile']['link']
);
$userData = $this->user_model->checkUser2($data);
var_dump($data);
// Send data to profile page
$this->load->view('admin/profile.php', $data);
}
else {
// Store users facebook login url
$data['login_url'] = $this->facebook->getLoginUrl();
$this->load->view('auth/profile.php', $data);
}
with model :
function checkUser2($userData){
$this->db->insert('tbl_oauth', $userData);
return $this->db->insert_id();
}
here is my table screenshot details:
I have to create a REST API for mobile backend using CakePHP. After following the instructions here I have been able to setup the requirements for creating a REST API. The problem is that I am getting a null in my output when I use the _serialize variable. Here is my code:
class InvitationController extends AppController {
public $components = array('RequestHandler');
public function index() {
}
public function add() {
if ($this->request->is('post')) {
$this->loadModel('Organizer');
$numPeople = $this->request->data['numpeople'];
$share = $this->request->data['myshare'];
$organizer = $this->request->data['organizer'];
$organizerMobile = $this->request->data['organizermobile'];
$this->set('organizerMobile', $organizerMobile);
$deadline = $this->request->data['deadline'];
$links = array();
date_default_timezone_set("Asia/Calcutta");
$now = date('Y-m-d');
$lastOrganizer = $this->Organizer->find('first', array(
'order' => array('Organizer.id' => 'desc')
));
$checkOrganizer = $this->getOrganizerDetails($lastOrganizer);
$pass = //something;
// save data in organizers table
$this->organizer->create();
$this->organizer->save(
array(
'name' => $organizer,
'mobile' => $organizerMobile,
'p_id' => 1,
'share' => $share,
'deadline' => $deadline,
'password' => $pass,
'group_cardinality' => $numPeople,
'created_on' => $now,
)
);
$message = 1;
$this->set(array(
'message' => $message,
'_serialize' => array($message)
));
}
}
}
When I make a request to POST /invitation.json I get null in the ouput with status 200. On the other hand if I simply do echo json_encode($message) in the Controller or in the View I get the correct output. I think I am doing something wrong. Please help!
That's wrong:
'_serialize' => array($message)
Pay attention to the manual, it shows it correctly in the provided examples.
'_serialize' => array('message')