PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 7th, 2013, 04:32 AM
paulh1983 paulh1983 is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Dec 2004
Posts: 2,222 paulh1983 User rank is First Lieutenant (10000 - 20000 Reputation Level)paulh1983 User rank is First Lieutenant (10000 - 20000 Reputation Level)paulh1983 User rank is First Lieutenant (10000 - 20000 Reputation Level)paulh1983 User rank is First Lieutenant (10000 - 20000 Reputation Level)paulh1983 User rank is First Lieutenant (10000 - 20000 Reputation Level)paulh1983 User rank is First Lieutenant (10000 - 20000 Reputation Level)paulh1983 User rank is First Lieutenant (10000 - 20000 Reputation Level)paulh1983 User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Weeks 1 Day 9 h 5 m 25 sec
Reputation Power: 201
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 05:19 AM.

Reply With Quote
  #2  
Old March 10th, 2013, 08:41 AM
navnav's Avatar
navnav navnav is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Location: Oxford, United Kingdom
Posts: 40 navnav User rank is Private First Class (20 - 50 Reputation Level)navnav User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Day 2 h 18 m 23 sec
Reputation Power: 1
Send a message via Skype to navnav
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,

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > Small class based on an API - need "evaluation"

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap