www.webdeveloper.com
Results 1 to 2 of 2

Thread: API layer for calling functions and instantiating a class and returning a json token

  1. #1
    Join Date
    Jun 2011
    Location
    California
    Posts
    383

    Lightbulb API layer for calling functions and instantiating a class and returning a json token

    Currently I have all my code on myintranetsite/login and it's hosted here on my server at home. What i'm looking to do is build an api layer on mysitecom/login that can use my php class on that site which is hosted else where. Is this possible and if so can anyone help me or point me in the right direction?

    I'm only looking to use the public function login on a different website that's using an entirely different server.

    My login form is really simple:

    HTML Code:
    <form id='register' action='http://mysitecom/profile' onsubmit="return validateForm()" method='post' accept-charset='UTF-8'>
    <fieldset>
    <legend>Log In</legend><br/>
    <label for='email' >Email Address*:</label>
    <input type='text' name='email' id='email' maxlength="50" /><br/><br/>
    <label for="password">Password*:</label>  
    <input type="password" name="password" placeholder="password" required><br/><br/>
    <input type='submit' name='Submit' value='Submit' />
    </fieldset>
    </form>
    Here is my php class:

    PHP Code:
    // Most objects in this framework are populated by calling the constructor, but
    // this one has a variety of entry points. They don't do any sanity checking
    // with eachother, so you can have $user->create and $user->register refer to
    // completely different rows.
    class Shopper extends Base {

        protected 
    $shopper_id;
        protected 
    $email;
        protected 
    $user_name;
        protected 
    $temp_token;
        protected 
    $sign_in_token;

        protected 
    $UserShoppingList;

        function 
    __construct($email null) {
           
            if (
    strpos($email'@') === false) {
                
    $this->sign_in_token $email;
            } else {
                
    $this->email $email;
            }
        }

        public function 
    activate($temp_token) {
            global 
    $db;

            
    $this->set_temp_token($temp_token);

            
    $vars = array();
            
    $vars[] = array(':i_temp_token'$this->get_temp_token());

            return 
    $db->get_function_as_proc('custom.japi_shopper_identity.Activate_User(:i_temp_token)'$vars) == 'Y';
        }

        public function 
    create($password) {
            global 
    $db;

            if (!
    $this->get_email() || !$this->get_username()) {
                return 
    false;
            }

            
    $vars = array();
            
    $vars[] = array(':email'$this->get_email());
            
    $vars[] = array(':username'$this->get_username());
            
    $vars[] = array(':password'$password);

            
    $id $db->get_function_as_proc('custom.japi_shopper_identity.create_user(:email, :username,  :password)'$vars);
            
    $this->set_id($id);

            return 
    true;
        }

        public function 
    get_email() {
            return 
    $this->email;
        }

        private function 
    get_id() {
            if (isset(
    $this->shopper_id)) {
                return 
    $this->shopper_id;

            } else if (
    $this->get_email())  {
                global 
    $db;

                
    $vars = array();
                
    $vars[] = array(':i_email_id'$this->get_email());

                
    $id array_pop(array_pop($db->get_function('custom.japi_shopper_identity.get_id_by_email(:i_email_id)'$vars)));

                
    $this->set_id($id);
                
    $this->shopper_id $id;
                return 
    $this->shopper_id;

            } else if (
    $this->get_sign_in_token())  {
                
    // todo: call get_id_by_token
                
    return false;
            }
        }

        public function 
    get_lists($clobber false) {
            global 
    $pd;
    //        $pd->print_object($this, 'User - has token?');
    //        $pd->print_object($this->get_sign_in_token(), 'Token');

            
    if ($this->UserShoppingList != null && !$clobber) {
                return 
    $this->UserShoppingList;

            } else if (
    $this->get_sign_in_token()) {
                global 
    $db;
                
    $pd->print_object($this'User - has token?');
                
    $pd->print_object(strtolower($this->get_sign_in_token()), 'token?');

                
    $vars = array();
                
    $vars[] = array(':i_sign_in_token'strtolower($this->get_sign_in_token()));

                
    $pd->print_object($this->get_sign_in_token(), 'About to seek lists using token');
                
    $rows $db->get_function('custom.japi_shopper_identity.get_lists_for_shopper(:i_sign_in_token)'$vars);
                
    $pd->print_object($rows'Rows returned by get_lists using token '.$this->get_sign_in_token());

                
    $this->UserShoppingList array_to_objects($rows'UserShoppingList');

                return 
    $this->UserShoppingList;
            } else {
                return 
    false;
            }
        }

        public function 
    get_sign_in_token() {
            if (
    $this->sign_in_token != null) {
                return 
    $this->sign_in_token;
            } else {
                return 
    false;
            }
        }

        public function 
    get_temp_token() {
            if (
    $this->temp_token != null) {
                return 
    $this->temp_token;
            } else {
                return 
    false;
            }
        }

        public function 
    get_username() {
            return 
    $this->user_name;
        }

        public function 
    json($obj null$return_json false) {
            if (
    $obj == null) {
                
    $obj $this;
            }
            return 
    parent::json($obj$return_json);
        }

        public function 
    login($password) {
            global 
    $db;

            if (!
    $this->get_email()) {
                return 
    false;
            }

            
    $vars = array();
            
    $vars[] = array(':i_email_id'$this->get_email());
            
    $vars[] = array(':i_password'$password);

            
    // $token = $db->get_function_as_proc('custom.japi_shopper_identity.login_by_username(:i_username, :i_password)', $vars);
            
    $token $db->get_function_as_proc('custom.japi_shopper_identity.Login_by_Email(:i_email_id, :i_password)'$vars);
            
    // todo: what if it's bad credentials?

            
    if ($token == null) {
                return 
    false;

            } else {
                
    $this->set_sign_in_token($token);
                return 
    $this->get_sign_in_token();
            }
        }

        public function 
    password_reset($tmp_token$password) {
            global 
    $db;

            if (
    strlen($password) < 8) {
                return 
    false;
            }

            
    $vars = array();
            
    $vars[] = array(':temp_token'$tmp_token);
            
    $vars[] = array(':new_password'$password);

            return 
    $db->get_function_as_proc('custom.japi_shopper_identity.password_reset(:temp_token, :new_password)'$vars) == 'Y';
        }

        public function 
    request_activation() {
            global 
    $db;

            
    $vars = array();
            
    $vars[] = array(':i_shopper_id'$this->get_id());

            
    $temp_token $db->get_function_as_proc('custom.japi_shopper_identity.activate_user_request(:i_shopper_id)'$vars);

            if (
    $temp_token == null) {
                return 
    false;
            } else {
                
    $this->send_activation_email();
                return 
    $temp_token;
            }
        }

        public function 
    request_password_reset() {
            global 
    $db$pd;

            if (!
    $this->get_id()) {
                return 
    false;
            }

            
    $vars = array();
            
    $vars[] = array(':shopper_id'$this->get_id());

            
    $temp_token $db->get_function_as_proc('custom.japi_shopper_identity.password_reset_request(:shopper_id)'$vars);
            if (
    $temp_token == null) {
                return 
    false;
            } else {
                
    $this->set_temp_token($temp_token);
                
    $pd->print_object('About to send the e-mail');
                
    $this->send_password_email();
                
    $pd->print_object('Sent the email');
                return 
    $this->get_temp_token();
            }
        }

        private function 
    send_activation_email() {
            if (!
    $this->get_email() || !$this->get_temp_token())  {
                return 
    false;
            }

            
    $fancy '
    <div style="text-align: center;"><img src="http://mysitecom/images/sitelogo.jpg" /></div>
    <h2>Welcome to site.com!</h2>
    <p>To complete your registration, <a href="todo: ">click here</a> or copy and paste the URL into your browser:</p>

    URL?token='
    .$this->get_temp_token().'

    Thanks!
    '
    ;

            
    $plain 'Welcome to site.com!

    To complete your registration, please activate your account by going to the URL below:

    URL?token='
    .$this->get_temp_token().'

    Thanks!
    '
    ;

            return 
    email_customer($this->get_email(), 'Welcome to site.com!'$fancy$plain);
        }

        private function 
    send_password_email() {
            global 
    $pd;
            
    $pd->print_object('In send_password_email');
            
    $pd->print_object($this->get_email(), 'E-mail');
            
    $pd->print_object($this->get_temp_token(), 'Token');

            if (!
    $this->get_email() || !$this->get_temp_token())  {
                return 
    false;
            }

            
    $pd->print_object($this->get_email(), 'Have all the data I need');

            
    $fancy '
    <div style="text-align: center;"><img src="http://mysitecom/images/sitelogo.jpg" /></div>
    <h2>Welcome to site.com!</h2>
    <p>To reset your password, <a href="todo: ">click here</a> or copy and paste the URL into your browser:</p>

    <p>URL?token='
    .$this->get_temp_token().'</p>

    <p>Thanks!</p>
    '
    ;

            
    $plain 'Welcome to site.com!

    To reset your password by going to the URL below:

    URL?token='
    .$this->get_temp_token().'

    Thanks!
    '
    ;
            
    $pd->print_object('About to actually e-mail');

            return 
    email_customer($this->get_email(), "Reset your site.com password"$fancy$plain);
        }

        public function 
    set_email($email) {
            return 
    $this->email $email;
        }

        public function 
    set_id($email) {
            return 
    $this->shopper_id;
        }

        public function 
    set_sign_in_token($token) {
            return 
    $this->sign_in_token $token;
        }

        public function 
    set_temp_token($token) {
            return 
    $this->temp_token $token;
        }

        public function 
    set_username($username) {
            return 
    $this->user_name $username;
        }

    I'm not entirely sure if this can be accomplished with just json or ajax and curl, all relevant information would be lovely.

  2. #2
    Join Date
    Jun 2011
    Location
    California
    Posts
    383

    Thumbs up

    Would something like this work for me?
    If that would work how do i integrate my login function into it?

    PHP Code:
    //extract data from the post 
    extract($_POST); 

    //set POST variables 
    $url 'mysite/login.php'
    $fields = array( 
    'email'=>urlencode($email), 
    'password'=>urldecode($password
    ); 

    //url-*** the data for the post 
    foreach($fields as $k=>$v) { $fields_string .= $k'='.$v'&'; } 
    rtrim($fields_string'&'); 

    //open connection 
    $ch curl_init(); 

    //set the url, number of POST vars, POST data 
    curl_setopt($chCURLOPT_URL$url); 
    curl_setopt($chCURLOPT_POSTcount($fields)); 
    curl_setopt($chCURLOPT_POSTFIELDS$fields_string);

    //excute post 
    $result curl_exec($ch); 

    //close connection 
    curl_close($ch); 

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles