Implementing an http API

Given an API of the form :

http://server.com/sendit?username=xxxxx&password=yyyyy&destination=zzzzzzz&message=mmmmmmm

The values of the parameters in the query string are dynamic, if they are supplied and run from the address bar of a browser
a message get sent.Now, what i want to do is create a form, get the values for the parameters from the form and pass them to
the http API via a php script, and have it run in the background instead of typing them directly to the address bar.

Let’s say $username=$_POST[‘username’]; etc

What’s the script going to look like.

Thanks.

Are you building this API or is it an existing API that you are trying to use?

If it’s the latter you could just create an HTML form that submits to the API.

Could you explain what exactly it is you are trying to do?

Ah right, sorry I misread and the browser bar example threw me off. :slight_smile:

Just one question, is the API on another server? e.g. - Google.

The if statements will look like this:


$username = $_GET['username'];

if(!isset($_GET['username']) {

   echo "Error message goes here.";

}else{

   'do something here'

}

The if statement checks whether the superglobal ‘username’ is set or not, if not it will display an error message. If it does then the ‘else’ part is used to process the desired code.

I am not getting values from the query string, rather i’m sending
values from the form

Well you don’t use POST, instead you will need to use GET:


$username = $_GET['username'];
$password = $_GET['password'];
and so on...

But you must do an if statement for each one to check if anything has been sent and it’s the correct data you require.

It’s sure going to help.

Thanks man.

With an HTML form that points the action to the above address using inputs with the same name as the parameters you need. No PHP required.

Its an existing API for connecting to a web sms server and sending sms once you supply the correct values in the query string. The values vary from user to user-dynamic.

The challenge is how to submit values from a form to the API within a script without having to type the whole thing into an address bar.

You have two options in that case.

If you want something super simple and quick, and if you’re the only person who’s going to use it, you can just create a form with the action set to the URL of the API. logic_earth explains this quite well.
The issue with this, the reason you can only use this option if you’re the only one using it, is that once you submit the form you’ll end up on API’s domain and all you’ll see is the response of the API, be it XML, JSON or whatever.

If you need to stay on your on application and want to display a human-friendly page/message you’ll need to submit the form to a PHP script that takes the values of the input fields of the form and submits it to the API. Then according to the response from the API you can display various messages. For this you might want to look into PHP’s file_get_contents() function. There are other methods too but this is the simplest one and should be sufficient for what I think you are trying to achieve.

I hope this helps! :slight_smile:

Yes, it’s on another server though not Google