开发者

PHP Web Services [duplicate]

开发者 https://www.devze.com 2023-03-19 17:15 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: Beginner to PHP web services
This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Beginner to PHP web services

I'd like to expose an API for my clients so that they can pull certain types of data back from my datastore, but have never done this in PHP. I have set up several web services in Apache CXF (in the 开发者_如何学GoJava side of the universe) but I absolutely need a PHP-based web service.

An initial Google has brought back an overwhelming number of leads, and so I figured it would probably be good to ask here first and see what are some APIs, open source libraries, etc. that the SO community would recommend. Also, any suggestions that are "best practices"-related would be greatly appreciated.

Thanks!


If you need a RestFul webservice I suggest you : http://www.recessframework.org/

It's an OpenSource code ( https://github.com/recess/recess ) and has a very good documentation/tutorial.


You can use JSON encoded Object/Array:

header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');

echo json_encode($data);

But before that you need to parse the request and do checks on the variable, maybe escape them to prevent any malicious data input or request. I suppose you'll need some simple API that will provide the access to the database. You can create your own class, something like class ProductsService {}

after that depending on the request items in the GET return different data.

domain.tld/api?action=getProducts&count=10&title=product title

parse the request, and return XML or JSON format to your client. If you want to protect the data and allow only your client to access it use predefined hash key and optional IP request check.

Here are two examples:

Zend Framework Service Class

PHP Soap Server class


I would just use the built in JSON capability, but if your API clients require SOAP/WSDL, I suggest the tried and tested NuSoap set of classes.


I know its not PHP based like you asked, but I was in the same boat not so long ago needed a web service, alot of the PHP frameworks out there didn't really cut it for me (unless you know ZEND which is OK at it but it's HUGE!) so I took at look at http://nodejs.org/ and found it very useful and quite easy to use.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号