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
Related
I am newbie to webservices
I have a domain class as below in my app.
class Training{
string name
Date date
}
and i want to display the list of training in php page
What changes i have to make in grails and in php script to do this.I want the data in JSON format.
And I want to follow the RESTful architecture.
Basically I want to interact with grails app from my website (written in php). In future i want to do save the data from my website to grails app DB and many calls.
And Do i need to authenticate before serving the request in webservice.
If you want to follow the architectural pattern see following link: http://grails.org/doc/latest/guide/webServices.html#REST
First create you TrainingController controller which performs the requested action and returns the json data. e.g.
class TrainingController {
def show = {
render Training.get(params.long('id')) as json
}
}
if you don't use a special url mapping to match the restful pattern, you can simply call training/1 and get the json structure returned. See the documentation for more rendering options.
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
I'm trying to do a call to a soap webservice from php.
The webservice returns an ADO.NET DataSet structure. Are there any libraries in PHP that can deal with this sort of data structures? If so, what are they called and where can I find them? If not, feel free to give tips?
So far, I have this (using ActiveMQ and the NuSoap library):
/**
* Create a new service instance
* Provide ActiveMQ uri and the extended class name
**/
$client = new Client('tcp://localhost:61613?tcpNoDelay=false', 'test');
/**
* New service reference
**/
$service = new ServiceProxy($client, 'ServiceName');
/* Service call */
$result = $service->get_clients();
get_clients() is a method that does the actual service call and it gets the DataSet structure in return. How can I manipulate this return value?
In .NET for example, there is a DataSet class. The ADO.NET DataSet contains one or more ADO.NET DataTables which in their turn consist out of one or more ADO.NET DataColumns and DataRows which are returned in a collection (array).
A simple code example, where the DataSet only contains one DataTable, can be:
/**
* Here, the val variable will contain the data positioned in the
* first field of the first DataRow of the first DataTable
**/
string val = dataset.Table[0].Rows[0].ItemArray[0];
I want to do the same in PHP, but I need a helping hand.
I would suggest you try learning more about the structure of the data you are working with. It is most likely some form of JSON or XML, which can be manipulated through the large set of PHP xml and json handling libraries.
Your first step is to look at the data being returned by your request and identifying it's format. Microsoft documents the ADO.NET API's datasets in depth HERE at the MSDN library. This should help you to make sense of what you are seeing when you inspect the data. Some data providers allow you to access the same data in different formats, depending on a parameter or a family of similar functions. Do you have documentation for your providers API?
Next, after you have identified the format and deciphered the specifics of the datasets schema, You need a class to manipulate the data. If you are dealing with something that conforms to a published standard you can use something like simpleXML, JSON or DOMXPath.This class should store the data in protected member variables and provide methods to inspect, iterate, load, refresh, search and so forth. You should refer to the PHP manual's function reference for help here and write whatever functions you need. I would write it generically to handle any similar dataset and derive a class to expose exactly the data I need for a particular application.
Another approach would be to write a COM component in a .net language to access and manipulate the data and import it's functions through PHP's COM extension. I think I would go with this choice if the data format is a weird proprietary Microsofty one.
The third possible approach works only if you have programming access to the server. If you can adapt the service provider to comply with the specification, Microsoft has released an interoperability toolkit which is supposed to act as a bridge between .net services and PHP clients using a layer of proxy objects to expose the ado.net data to PHP scripts. They throw the word RESTful around alot, but I'm not really sure what they mean by that. Check out the OData SDK for PHP HERE at Microsoft's interoperability HQ (there is info here about PHP and other Microsoft platforms and products like Azure, silverlight, Bing, etc., as well). As I said though, I think you would need the data provider to emit data which conforms to their standard. Perhaps it already does. I can't tell without a schema! If so then this is your best bet.
Good Luck!
I'm working with a .NET based Web Service where some of the API calls can be accessed via HTTP POST/GET but on others only SOAP 1.1/1.2 are available.
The company that has developed the API have come back to me have said the following and I was wondering if he's trying to pull the wool over my eyes or whether he's speaking the truth. His response doesn't sit comfortably with me so I thought I would try and check but haven't been able to find an answer.
I was thinking it is very odd as I
have not specified anything on the
other methods in order to make them
accessible using the HTTP Post or HTTP
Get protocols. I have globally set it
so that they should all be accessible.
I have been looking into this, and it
seems that the HTTP Post and HTTP Get
protocols can only be used with
methods that use simple types, e.g.
where a method takes and integer
parameter. The methods you have listed
below take more complex objects, e.g.
nullable type or custom objects.
However the SOAP protocol can be used
with this complex methods.
His explanation is correct. POST/GET only permits typical primitives -- int, string, bool, etc. Or rather, parameters that can be interpreted with simple name/value pairs. The SOAP protocol handles the XML side of web services in .Net (sounds like he's talking about legacy ASMX services.)
UPDATE: a little context here. It sounds like your contact doesn't know much about .Net web services, so while his description is based on what he knows, it's certainly not a limitation of the environment; he just needs to do a little more work.
He is incorrect. You can use the DataContractSerializer to send and GET complex types.
Here is some code I used to answer another question recently:
In order to POST parameters, you need to serialize it using the DataContractSerializer. e.g,
On server:
[OperationContract]
[WebInvoke(Method="POST",UriTemplate = "/foos")]
void PostFoo(Foo foo) {}
On client:
var foo = new Foo();
var content = HttpContentExtensions.CreateDataContract<Foo>(foo);
var client = new HttpClient("http://example.org/service.svc/foos");
client.Post(content)
The client code uses the Microsoft.Http library that is found in the WCF Rest Starter kit.
I am sending an NSData Object to the server script running PHP.Is it possible to collect that object in PHP ?
Is it possible to get NSArray,NSString, NSData or any Custom Objects (Subclass of NSObject) in PHP ?
Thanks
Look for a method for serializing and deserializing your data.
Facebook uses a protocol called Thrift Google uses protocol buffers.
If you don't need to have an industry standard solution, you can come up with your own way to tag values with meta data in order to determine how PHP will handle your data. PHP doesn't (at least that I'm aware of) have all the niftness that objective C has with its objects and inheritance, but PHP has a function or library for almost anything you will need to do.
One approach might be to use JSON to store type/value pair for each parameter you want to process.