Creating a PHP API?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AaronL
    New Member
    • Jan 2007
    • 99

    Creating a PHP API?

    Hello,

    I am so close to getting my big software project done, but I'm not satisfied on how one aspect works. The deal is, I want to have people use my software as a service without gaining access to my source code. I am to understand that if I develop my software as an API, I can do this.

    My questions are:

    How do I make an API in PHP?
    - How do I send parameters to the API and get data back?

    Here is my code on how I'm currently getting data from my software:
    Code:
      // *** REQUIRED CODE BY LAMBERT SOFTWARE ***
      $ecommfetch = 'http://www.ecommphppro.com/' . $companycode . '/';
      
      // Get the function parameter
      if ($_GET["fct"]!='')
      { $function = $_GET["fct"]; }
      else
      { $function = 'catalog'; }
      
      // Determine which function we need from eCommPHP Pro
      switch ($function)
      {
        case 'catalog':
          $ecommfetch .= 'catalog.php';
          break;
        
        case 'viewitem':
          $ecommfetch .= 'catalog.php';
          $parameters .= '&fct=viewitem';
          break;
      }
      
      // Now that we have the function, lets process the parameters passed.
      if ($_GET["scat"]!='')
      { $parameters .= '&scat=' . $_GET["scat"]; }
      
      if ($_GET["pg"]!='')
      { $parameters .= '&pg=' . $_GET["pg"]; }
      
      if ($_GET["ppg"]!='')
      { $parameters .= '&ppg=' . $_GET["ppg"]; }
      
      if ($_GET["srt"]!='')
      { $parameters .= '&srt=' . $_GET["srt"]; }
      
      if ($_GET["srch"]!='')
      { $parameters .= '&srch=' . $_GET["srch"]; }
      
      if ($_GET["inum"]!='')
      { $parameters .= '&inum=' . $_GET["inum"]; }
      
      if ($_GET["vimgnum"]!='')
      { $parameters .= '&vimgnum=' . $_GET["vimgnum"]; }
      
      // Now lets make sure the right delimiter is in place for the function
      if (substr($parameters, 0, 1)=='&')
      { $parameters = '?' . substr($parameters, 1); }
      else
      { 
        if ($parameters!='')
        { $parameters = '?' . $parameters; }
      }
      
      // Add the parameters to the fetch URL
      $ecommfetch .= $parameters;
      
      // Replace spaces with +
      $ecommfetch = str_replace(" ", "+", $ecommfetch);
      
      // Go ahead and fetch the page now.
      echo trim(file_get_contents($ecommfetch));
    Is there a way to do this better and not use
    file_get_conten ts? I want to create an asp compatible
    way of getting data too so that I don't have to limit
    my customers to just PHP servers.

    If someone can explain this to me in details, I would
    be forever grateful.
  • kovik
    Recognized Expert Top Contributor
    • Jun 2007
    • 1044

    #2
    Building an API on the web is basically a means of allowing developers to access data the same way that they would through a web browser: requests and responses.
    • One way of accomplishing this is to allow server requests and responses where the developer posts data and receives data back, in the style of AJAX.
    • Another way is to allow socket connections for requests and responses. This is more responsive for the developer (though possibly less friendly to newer developers), but potentially more server intensive for you.


    These methods shouldn't be language-specific.

    Comment

    • AaronL
      New Member
      • Jan 2007
      • 99

      #3
      I see, what is AJAX exactly, I see that out there a lot.

      Comment

      • kovik
        Recognized Expert Top Contributor
        • Jun 2007
        • 1044

        #4
        AJAX is basically a method, using JavaScript, to request information via a HTTP request and receive a response. IT's called AJAX (Asynchronous JavaScript And XML) because the standard response used to be an XML document, though many developers are using JSON objects now.

        Comment

        Working...