I'm trying to access a Lithium Forum Rest API using Zend_Rest_Client. The API is described here:
http://cl.ly/3N0M2D0k0H3L0Y103Q3R
The API docs give example calls such as e.g. /boards/id/experimentations/messages/id/938
How can I replicate this call Using Zend Rest Client and specifying each individual method / parameter?
I can get a response by putting the entire URL in my new Zend_rest_Client($url) call but I want to have more than control so that I can pass dynamic board and category ids.
I have tried something like
$result = $client->arg('categories')->arg2('id')->arg3($cat->string_id)->arg4('boards')->arg5('nested')->get();
and also
$result = $client->categories($cat->string_id)...
But the parameters are ignored. Can anyone shed any light on this?
If anyone wanted to try it, there is a live API here:
http://lithosphere.lithium.com/lithium/restapi/vc/boards/nested
looks like this particular API is not compatible with Zend_Rest_Client as it doesn't seem to support methods and parameters in the URI as the client expects. The solution is to build dynamic URLS to call via GET or use POST parameters
Related
I am using Slim framework to create an REST API for my project. The WebApp itself is developed using ReactJS.
I have a doubt regarding the same. I have a set of values stored in an array in my ReactJS App. These values are filters which is used ti filter data from my table.
My table contains a field called location. In my WebApp I have search filter were I can add multiple locations based on which the results have to be displayed. I store these filters in an array.
I want to know how can I pass this array to Rest API Route so that I can get this array in my REST API Framework.
I want to achieve something like
/routes/location/[arrayOfValues]
There are only two ways of passing data to the server - either as URL params (for POST, GET, or anything else) or in the request body (for PUT or POST). Are you also writing the code to handle the API request? From your description of the use case, it sounds like GET would be the most semantically correct verb, so you you should use URL params as #BrianPutt suggested. Using square brackets is the standard way to encode an array value. How are the locations defined? Longitude/latitude? If so, you could also just build a comma-separated string, e.g.
/routes/location?q=12.3409,-40.0004,2.34004,120.0294
But odds are your back end framework has built-in support for parsing those URL params, so you should just use that.
/routes/location?q[]=12.3409,-40.0004&q[]=2.34004,120.0294
The only reason not to do this is if you have a huge number of locations, in which case you might want to use a POST request and build some kind of JSON object to put in the body.
Also: be consistent! If the rest of your API uses JSON, you should probably use it here too.
I'm currently working on a project that requires me to call an external API via HTTP to get some data in the form of JSON. This data will be saved to a database pattern defined with the Eloquent ORM. This API requires authentication (by token) and then accepts calls with GET, POST is not needed. The parameters are added to the URL (e.g. ?origin=LHR&destination=GHA).
I'm trying to find a Laravel way to access such data easily - just writing a basic class that has a function with all parameters doesn't seem right. I'd much rather have some sort of query builder, but for the URL. I looked at repositories, but that seems to be geared towards database calls.
To sum up: is there any good "Laravel way" to call an external API?
I've dene something like this just few days ago;
My solution was to define array GET params. F.a.
get-some-stuff?where[foo]=bar&with[relation]&with[otherRelation]
Then you can get the params via Input::get() and go through them with an foreach.
F.a.
foreach(Input::get() as $method => $value) { ...
In the foreach you could decide what to do based on $method
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Framework for providing API access to website?
I need to write a service in PHP. The server will be used by android/iphone clients through the url for example like this
http://www.myservice.com/query.php?param1=a¶m2=2...
The server will return data back
The client will push data to server
There can be large num of clients simultaneously accessing so the performance is key
I want to use the data format that is easily understood by my android client. In other words, I do not want to reinvent the wheel and create my own format and parsing, instead I would prefer to use any library if it exists.
Is there a framework that I can use to abstract the communication mechanism for data get and push ?
Thanks,
Ahmed
I developed a class that is the PHP native SoapServer class' REST equivalent.
You just include the RestServer.php file and then use it as follows.
class Hello
{
public static function sayHello($name)
{
return "Hello, " . $name;
}
}
$rest = new RestServer(Hello);
$rest->handle();
Then you can make calls from Java like this:
http://myserver.com/path/to/api?method=sayHello&name=World
(Note that it doesn't matter what order the params are provided in the query string. Also, the param key names as well as the method name are case-insensitive.)
Get it here.
Being the Author of Restler, I would like to suggest that you can try Restler 3 for the following reasons
It is specifically made for API creation
It handles media type conversion for you and supports many media types including
JSON
Plist (both XML and Binary)
XML
Comes with many examples to get started.
Your API is automatically documented with Restler API Explorer
well,
Use mvc framework(yii, ci, ...)
and from controller directly print json_encode($object).
It will return json data to browser, and consume it anywhere(compuer, droid, iphone, ... , iron :D). the solution is json. just share object in json format, so anyone can map it into it's preferred obejct
here is something something you may have a look
I'm a new user at REST architecture, now I'm trying to make a test with my API.
I like to call this method in the url but I don't know the right format.
My concern is that I have two parameters to be sent by post request.
public function CreateExemple ($name, $libelle)
{
}
This is always be a good practice to use a ready to use package if available. There are lots of RESTful APIs available to use. It looks like you are using PHP for development. you can start with :
Zend REST
Over the last few weeks I've been learning about iOS development, which has naturally led me into the world of APIs. Now, searching around on the Internet, I've come to the conclusion that using the REST architecture is very much recommended, due to its supposed simplicity and ease of implementation.
However, I'm really struggling with the implementation side of REST. I understand the concept; using HTTP methods as verbs to describe the action of a request and responding with suitable response codes, and so on. It's just, I don't understand how to code it.
I don't get how I map a URI to an object. I understand that a GET request for domain.com/api/user/address?user_id=999 would return the address of user 999 - but I don't understand where or how that mapping from /user/address to some method that queries a database has taken place.
Is this all coded in one PHP script? Would I just have a method that grabs the URI like so:
$array = explode("/", ltrim(rtrim($_SERVER['REQUEST_URI'], "/"), "/"))
And then cycle through that array, first I would have a request for a "user", so the PHP script would direct my request to the user object and then invoke the address method. Is that what actually happens?
The main thing I'm not getting is how that URI /user/address?id=999 somehow is broken down and executed - does it actually resolve to code?
class user(id) {
address() {
//get user address
}
}
Actually the API you're trying to describe now is not RESTful. There are many sources describing how to make RESTful APIs. So you should first define your API (taking in account how your client software would use it) and then implement it. I'm quite sure that almost any RESTful API can be implemented in PHP.
Here are some other tips on how to make a RESTful API.
In my opinion GlassFish Server REST Interface is a good example of RESTful design.
That's two questions.
To honor RESTful HTTP verbs, you have to query $_SERVER["REQUEST_METHOD"]. It will contain the usual GET or POST unless a more specialized HTTP request was received. The whole REST buzz is somewhat misleading in itself, and also in misusing the HTTP verbs just for routing.
Anyway, the mapping of request URLs to functions can be achieved in two ways. It's most reliable to use a static map, for example an array that lists URL patterns and destination methods. Most PHP frameworks use an implicit mapping like so:
$args = explode("/", trim($_SERVER['REQUEST_URI'], "/"));
$class = array_shift($args);
$method = array_shift($args);
call_user_func_array("$class::$method", $args);
Note how this is a bad example, security-wise. Only allowed and specifically prepared classes and methods should be able to receive requests. Most frameworks just check if it was derived from an acceptable base class after loading it from a known path and/or instantiating. But a static map is really preferable.
Anyway, regular expression mapping or handling by mod_rewrite is also common. And for utilizing HTTP verbs, just include it as method name.
Have a look at FRAPI - http://getfrapi.com/
As it says focus on your business logic, not presentation.