by brentb
10 replies
I know php pretty well but have never set up an API. I know I know, can't believe I have survived so long. Not a lot of great documentation that I could find that is basic so I can 'get' the concept. But more in depth than API secret then server sends info to server and then back.

What i do know about communicating to other servers is this:

1. You can access other server scripts by just doing <img> or <iframe> to the code with appended variables.

2. You can submit via cURL.

How does it work in API to communicate back and forth? I am pretty sure its not either of these ways.

Also if there is a very simple API for facebook or twitter or something easy for me to learn on, any suggestions appreciated.
#and% #api
  • Profile picture of the author tajimd
    When using img or iframe, what happens is, the other server sends you fully formatted output which you can just plug into your own site.

    When using API.. the other server sends the output result in JSON or XML format which you have to then format using your own PHP, HTML and output into your site.

    So, if you know PHP then go ahead and learn how to output JSON and XML results in PHP and once you know that then it becomes pretty easy to work with any API.
    {{ DiscussionBoard.errors[7219233].message }}
  • Profile picture of the author SteveSRS
    IMG and iframe has really nothing to do with API's.

    API's can be XML, JSON (as mentioned before) but also plain text, your own protocol, using the full array of webserver commands (GET, POST, PUT, DELETE).

    The most simple api setup would be:
    www.server.com/api.php?command=getdata

    Where you would be using $_GET for data input and just echo data out to reply.
    Of course this is NOT the recommended method and you must ALWAYS verify input data and never trust it.

    Recommended: JSON-RPC - works awesome!
    {{ DiscussionBoard.errors[7219464].message }}
  • Profile picture of the author eminc
    Hey,

    API works just like normal PHP code, difference being that you can input/output using any other programming language. Mostly conversion tracking and other things are done by Javascript, so people prefer using JSON as one of the methods of passing variables.

    You can make api work like this:

    1) Let the user get authenticated by putting a request, something like:

    http://yoursite.com/api/auth.php?una...&secret=secret

    2) at the auth.php file, authenticate them, and set session variables, User's IP and Username and give them a token (a random string ). Store that token in session variable too.

    3) For all the other request they make, verify the token, and the IP Address for the request, and get on with the query. Note that you may have to set a time out for their session too

    Just kept it simple, you can obviously add some other things to make it more secure

    iframes and img tags are just the same, they access the same script, get the same thing done. Some are used for a particular task e.g. iframes for displaying buttons that involve interaction on some other domain(facebook likee, tweet), img tags for pixel tracking etc.


    Mohit
    {{ DiscussionBoard.errors[7220034].message }}
    • Profile picture of the author viescripts
      For APIs CURL is used anyway.

      There's a param for CURL curl_setopt:
      CURLOPT_RETURNTRANSFER

      If this param is set as true, the curl_exec command returns back the output.

      Now the API owner decides what format they print data on their side plain text, JSON, XML etc.
      {{ DiscussionBoard.errors[7220091].message }}
  • Profile picture of the author brentb
    So to request the API I should use cURL to make the request and grab the output, however I should be prepared for a json or xml output?

    Thats basically API101 in a nutshell?
    {{ DiscussionBoard.errors[7220797].message }}
    • Profile picture of the author SteveJohnson
      Originally Posted by brentb View Post

      So to request the API I should use cURL to make the request and grab the output, however I should be prepared for a json or xml output?

      Thats basically API101 in a nutshell?
      No. That's not API101 in a nutshell.

      First, read this: Application programming interface - Wikipedia, the free encyclopedia

      You'll see that an API is nothing other than a set of rules that a site sets up in order to allow access to some portion of its stored data or functionality. Nothing more. A site publishes directions on how its API works - you follow those instructions when you're programming whatever it is you're using their data for.

      HOW you access that data or functionality is up to the author of the API. Some sites let you make a simple HTTP request to a certain URL using GET: http://somesite.com?param1=all&param2=none

      Some make you use POST requests.

      Either can be accomplished within your own script by using cURL, if you're using PHP. You make the request, get the response, and do something with it.

      The key is, you ALWAYS know what form and structure the response will have, because the sponsoring site has told you in its description of its API. You never have to guess, you never have to 'be prepared for json or xml output'.

      Check the Amazon Product Advertising API at https://affiliate-program.amazon.com...tail/main.html, or PayPal's x.com for places to get your feet wet.
      Signature

      The 2nd Amendment, 1789 - The Original Homeland Security.

      Gun control means never having to say, "I missed you."

      {{ DiscussionBoard.errors[7221506].message }}
      • Profile picture of the author viescripts
        Originally Posted by SteveJohnson View Post

        No. That's not API101 in a nutshell.

        First, read this: Application programming interface - Wikipedia, the free encyclopedia

        You'll see that an API is nothing other than a set of rules that a site sets up in order to allow access to some portion of its stored data or functionality. Nothing more. A site publishes directions on how its API works - you follow those instructions when you're programming whatever it is you're using their data for.

        HOW you access that data or functionality is up to the author of the API. Some sites let you make a simple HTTP request to a certain URL using GET: http://somesite.com?param1=all&param2=none

        Some make you use POST requests.

        Either can be accomplished within your own script by using cURL, if you're using PHP. You make the request, get the response, and do something with it.

        The key is, you ALWAYS know what form and structure the response will have, because the sponsoring site has told you in its description of its API. You never have to guess, you never have to 'be prepared for json or xml output'.

        Check the Amazon Product Advertising API at https://affiliate-program.amazon.com...tail/main.html, or PayPal's x.com for places to get your feet wet.
        formally we can call API - any post or get action in our browser window
        {{ DiscussionBoard.errors[7234099].message }}
    • Profile picture of the author viescripts
      Originally Posted by brentb View Post

      So to request the API I should use cURL to make the request and grab the output, however I should be prepared for a json or xml output?

      Thats basically API101 in a nutshell?
      yes, this is the basic idea of API.
      You post to API via CURL by POST or GET method a series of variables and values: API passphrase, some other data and get back from them the result, then just decode it.

      All try to make now API as a set of standards, but all basicly work the same way.
      {{ DiscussionBoard.errors[7234090].message }}
  • Profile picture of the author FirstSocialApps
    Some half correct info here posted by the other users:

    API = Application Programmers Interface , in the context of web development its just a set of 'rules' to interact with another site. Nothing more.
    {{ DiscussionBoard.errors[7234550].message }}
  • Profile picture of the author Glen Barnhardt
    Wow, Yes an API is simply a Application Programmers Interface. How it works depends on the site offering the API. The goal often being to receive data and sometimes update.

    The reason for an API is to allow programming that interacts with the other systems Site. Google for instance has API's for a number of things.

    The Google Maps API allows you to create Maps on your site. You send information about a location and it sends you information back to build the map.

    I've worked with API's on Facebook, Twitter, Google, Aweber, PayPal, Authorize.Net, and at this point I can't even remember the rest. But not one of them works exactly the same. So you have to learn how the API on that system works and then create the code to use it.

    Some API's can be tested with a browser. These allow for URL parameters to be sent and the data will be returned.

    The best thing to do is find the specific API your interested in working on. Then look at the documentation.

    Try creating an interface to work with it. When you have problems then post your questions.
    {{ DiscussionBoard.errors[7236205].message }}

Trending Topics