#1
  1. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Dec 2004
    Posts
    3,027
    Rep Power
    377

    Small class based on an API - need "evaluation"


    I am creating a class for the first time so wanted your thoughts on what i have so far.. (its not complete) as i do not want to go down the wrong road if what i have done is not ok...

    if you need the API then please let me know..

    thanks

    PHP Code:
    class API {
        
        public 
    $method "POST";
        public 
    $content_type "application/x-www-form-urlencoded";
        private 
    $key "key";
        private 
    $media_code_id "";
        
        private 
    $api_errors = array (
            
    "Invalid API Key"   => "The API Key provided is invalid",
            
    "Not authorized"    => "Your IP Address is not authorised to access this API."
        
    );
        
        private 
    $errors = array();
        
        private 
    $parameters = array (
            
    'car_id'        => array ('label'=>  "Car ID",          'required'=>'yes',  'type' => 'Integer''possible_values' => array (402451604)   ),
            
    'title'         => array ('label'=>  "title",           'required'=>'yes',  'type' => 'String',    ),
            
    'firstname'     => array ('label'=>  "firstname",       'required'=>'yes',  'type' => 'String',    ),
            
    'surname'       => array ('label'=>  "surname",         'required'=>'yes',  'type' => 'String',    ),
            
    'email'         => array ('label'=>  "email",           'required'=>'yes',  'type' => 'String',    ),
            
    'tel'           => array ('label'=>  "tel",             'required'=>'yes',  'type' => 'Integer',    ),
            
    'postcode'      => array ('label'=>  "postcode",        'required'=>'yes',  'type' => 'String',    ),
            
    'address_1'     => array ('label'=>  "address_1",       'required'=>'yes',  'type' => 'String',    ),
            
    'address_2'     => array ('label'=>  "address_2",       'required'=>'no',   'type' => 'String',    ),
            
    'town'          => array ('label'=>  "town",            'required'=>'no',   'type' => 'String',    ),
            
    'county'        => array ('label'=>  "county",          'required'=>'yes',  'type' => 'String',    ),
            
    'optin-tel'     => array ('label'=>  "optin-tel",       'required'=>'no',   'type' => 'Integer',  'possible_values' => array ('1''0')   ),
            
    'optin-email'   => array ('label'=>  "optin-email",     'required'=>'no',   'type' => 'Integer',  'possible_values' => array ('1''0')   ),
            
    'optin-sms'     => array ('label'=>  "optin-sms",       'required'=>'no',   'type' => 'Integer',  'possible_values' => array ('1''0')   ),
            
    'optin-post'    => array ('label'=>  "optin-post",      'required'=>'no',   'type' => 'Integer',  'possible_values' => array ('1''0')   ),
            
    'type'          => array ('label'=>  "type",            'required'=>'yes',  'type' => 'String',   'possible_values' => array ('brochure''testdrive','motability')   ),
            
    'media_code_id' => array ('label'=>  "media_code_id",   'required'=>'yes',  'type' => 'Integer',    ),
        );
        
        private 
    $url "url";
        private 
    $media_code_url "mediacodes/create";
        private 
    $post_contact_url "contacts/create";
        private 
    $post_data ""//used to hold user data that needs to be sent across into the request. this has been run through the function http_build-query..
        
    private $user_input ""//this is the actual data from the user

        
    public function __construct($data){
            
    $this->user_input $data;
        }
        
        public function 
    check_user_input(){
            
    //if the key doesnt exist in parameter, throw it away!
            
    foreach ($this->user_input as $key => $value){
                if (!isset(
    $this->parameters[$key])){
                    unset(
    $this->user_input[$key]);
                }
            }
            
            foreach (
    $this->parameters as $key => $options){
                if (  
    $options['required'] == "yes" && !isset($this->user_input[$key]) ){
                    
    $this->errors['required'][] = $options['label'];
                } else if ( 
    $options['type'] == "Integer" && !is_integer($this->user_input[$key])){
                    
    $this->errors['Integer'][] = $options['label'];
                } 
            }
        }
        
        
        
    //formats data to be able to send it as a request
        
    public function format_data($array){
            return 
    http_build_query(
                array(
                    
    $array
                
    )
            );
        }
        
        public function 
    create_header(){
           return  
    stream_context_create(
                array(
    'http' =>
                    array(
                        
    'method'  => $this->method,
                        
    'header'  => "Content-type:".$this->content_type."\r\n".
                         
    "X-API-KEY: \r\n",
                        
    'content' => $this->post_data
                    
    )
                )
            );
        }
        
        public function 
    post_request(){
            return 
    json_decode file_get_contents(''false$context) );
        }

    Question:

    for checking the user_input, should i do:

    PHP Code:

    loop through parameters

         
    if parameter_key user_input_key 
    OR

    PHP Code:

    loop through user_input

        
    if  user_input_key parameter_key 
    Last edited by paulh1983; March 7th, 2013 at 06:19 AM.
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Location
    Oxford, United Kingdom
    Posts
    40
    Rep Power
    3
    That all seems okay to me. And for your question, do it like this (simply because it's more clearer (to me, atleast)):

    PHP Code:
    if ( parameter_key == user_input_key 

    Comments on this post

    • paulh1983 agrees : thanks for your help. sorry for late reply,

IMN logo majestic logo threadwatch logo seochat tools logo