Organizing a API connection class

Hello professors,

This is for an application that will not have the API as the only goal. It will perform more things and, among them, work with a given API.

So,

class Apiconnect {
    
    
       const URL = 'https://someurl.com/api.php';
       const USERNAME = 'user';
       const PASSWORD = 'pass';
    
        /**
         *
         * @param <array> $postFields
         * @return SimpleXMLElement
         * @desc this connects but also sends and retrieves 
               the information returned in XML
         */
        public function Apiconnect($postFields)
        {
            
            $postFields["username"] = self::USERNAME;
            $postFields["password"] = md5(self::PASSWORD);
            $postFields["responsetype"] = 'xml';
    
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, self::URL);
            curl_setopt($ch, CURLOPT_POST, 1);
    	    curl_setopt($ch, CURLOPT_TIMEOUT, 100);
    	    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    	    curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
    	    $data = curl_exec($ch);
    	    curl_close($ch);
    
    	    $data = utf8_encode($data);
    	    $xml = new SimpleXMLElement($data);
            
    	if($xml->result == "success")
    	{
           return $xml;
    	}
        else
        {  
          return $xml->message;
    	}
      }
    
    }

On another file I have the following:


abstract class ApiSomething
{
    
  protected $_connection;
  protected $_postFields = array();
    
  /**
   * @desc - Composition.
   */
  public function __construct()
  {
     require_once("apiconnect.php");
     $this->_connection = new Apiconnect($this->_postFields);
  }
  public function getPaymentMethods()
  {
   //this is the necessary field that needs to be send. 
   //Containing the action that the API should perform.
   $this->_postFields["action"] = "dosomething";
   
    if($apiReply->result == "success")
    {
      //works the returned XML
      foreach ($apiReply->paymentmethods->paymentmethod as $method)
      {
        $method['module'][] = $method->module;
        $method['name'][] = $method->displayname;
      }
     return $method;
    } 
   }
}

SO, on this getPaymentMethods I need to grab $_postFields class property here, so that I can send this “action” but I’m not getting how.

Can I have a push please?

Márcio

Ok… that’s a start. So:

Additional Note 1:
I don’t need to abstract the API CURL stuff because, that will be the only CURL operations that I will EVER need to perform for this application life time.

Additional Note 2:
I will not use any other API’s what so ever, so (I believe) that implementing an interface will not be appropriate).

Additional Note 3:
All methods on ApiSomething will, for sure, require the connection. However, I don’t want to connect each time I need to use it…

@logic_earth - Not sure if I get what you are telling me.

if you notice for the constructor for ApiConnect you start cURL, running it…then exiting it when you do not have all the data for it to be running

All the data that I need to run the connection is there so I believe … :s

I notice however that THAT argument $postFields on the constructor is of NO use so I believe, because we need not to receive any information here. It’s all on this class…

hmm…

I’ve look at your previous post code addition. What I’m not yet getting why should we (and I have heard it a lot on the couple hours) to separate the “setup part” and remove the “execution part” from the constructor? I realise that you have, more or less trying to answer this question, but I’m still not getting it… :s yet :D?

Well…if you notice for the constructor for ApiConnect you start cURL, running it…then exiting it when you do not have all the data for it to be running. There is your problem. You can set it up but you should not be running it.


<?php
 
class ApiConnect
{
  const
    URI      = '...',
    USERNAME = '...',
    PASSWORD = '...';
 
  protected
    $curl,
    $fields;
 
  public function __construct ()
  {
    $this->fields = array(
      'username'     => '...',
      'password'     => '...',
      'responsetype' => '...'
    );

    $this->curl = curl_int();
    curl_setop( $this->curl, ... );
    # ...
  }

  public function execute ( $fields )
  {
    $fields = array_merge( $this->fields, $fields );
    $data = curl_exec( $this->curl );
    # ...
  }
}