Jump to content

Writing an API.


gerkintrigg

Recommended Posts

If I want other people to access my PHP code from THEIR website, do I need to write a clever API handler, or is there a simple way of allowing other people to access it?

 

(I had a look and thought this was the most likely forum, so sorry if it needs moving.)

 

Any suggestions welcome.

Link to comment
Share on other sites

If you want other peopple to access your code, just put it up as a download on your site.

 

Unless what you mean is, you want other people to be able to access functionality provided by your code. Yeah, that's what API is for.

Link to comment
Share on other sites

Sorry Mchl, yes - I want them to access the functionality, but not the code itself.

 

As far as I see it, there will need to be 2 parts to it... One for my side, that resides on my server so that the API app can plug into it and one for the client, which will need to be able to interface with the web server.

 

How does one go about starting to write an API? Is it able to be done in PHP and if so, are there any tutorials on how to get started?

 

I once wrote something back in the 90's in Object Pascall which interfaced with a web server, but that was before PHP hit the big-time and it used a JAVA engine, so I'm a bit clueless... it was a LONG time ago...

Link to comment
Share on other sites

Without any knowledge on specifics for API's;

 

I would use this method:

 

1. Create a script that uses special codes and restrictions, so only it's "Clients" can access functionality.

2. Use the script like so:

Webserver sends a request using a "Client Class Object" (that you create for your clients), that sends either a POST or GET type request (POST is preffered). In the form of:

http://ip.add.re.ss/clientAPI/?method=[CLASS::METHOD]&param1=[Data]&param2=[Data]&...

 

The page would output the response, and the Client script would read it as the result.

Alternatively you could risk using EVAL() (Highly Risky), if though, you do you eval, you can send arguments in raw php code, to be executed by the script. Though if any security issues arose from this method you could lose your entire server (not just your website).

 

Other than that, i wouldn't be able to help you im afraid, but good luck i'll keep an eye on this thread ;).

 

-CB-

Link to comment
Share on other sites

Rememberm that API is a very general term, and there might be many specific implementations.

 

For web application API is handled through POST/GET requests. The client will send to your server a request (usually specyfing some action to perform, any input data if needed and optionally an 'API key' if the service is for authorised users only. Your script does its job, and hands the results back to the client. Now, whether it'll be JSON, XML or even plain text, is up to you.

 

If you have some experience with AJAX, you might find it familiar. An AJAX application is basically a JavaScript application on client side that uses API to communicate with server side applicaiton.

Link to comment
Share on other sites

There are different ways to approach this. It all depends on your needs. You can do a simple POST/GET API like suggested above. You could also have a RESTful API where parameters get passed like: http://yourdomain.com/param1/param2/param3/ (this will need mod rewrite rules on your server). You can do a combo of POST/GET and REST by POST/GET to a URL like the one I showed above. You can also look into a SOAP server. There is VERY LITTLE documentation on this, and it was such a pain to setup the WSDL for it. I wrote one a few months ago which works really well, but be warned its very challenging. If I had to do it again I would use REST combo with POST/GET.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.