We are rolling out one of our services to another service provider in PHP. They have already built the client, it just sends data in a post (no xml/json etc). Our script then processes it and returns an xml string with the response. Also they will need a token authentication system. Because of the fact they are just using cURL to post raw data, I don't think I can use soap/rest/xml-rpc ... can anyone point me to any good tuts etc?
Cheers,
Oauth is a secure and easy to implement for security solution for tokens. And they have libraires for java, php, python, and pretty much any language you can think of.
Very strange (and backwards) they built a client without an existing service. but the important thing is to document the interface between your two systems, and adhere to it.
David Walsh explains a simple php web service that returns JSOn or XML.
http://davidwalsh.name/web-service-php-mysql-xml-json
Related
I'm having troubles to figure out how web services handle requests and send responses, and how my site/module will handle it. So these are my questions and bellow a little about why i am asking this:
What is the right way to send a request to web service?
How a web-service will usually answer a site requesting?
How a PHP site can send a request to a web-service? Is cURL the right way?
I'm a student who are learning PHP and a lot of other things, and my job now is create a Joomla Module to show information from a web service (probably created in java or something, probably created by me, when i learn java...).
I know i will use http requests to talk with the web service, but i worry im using it wrong (making a request to the url, like the browser).
So, i did a little example site and a example API. The api uses Slim microframework to create routes, access my database and return the content in json (what means: if i access 'api.com/api/something' in my browser i see a plain white page with a lot of json). I pretend this is my web service.
The example site send a request to the API with cURL, reads the content and decode the json and do things. It seems forced to me.
I have not much support to understand web services and if i am in the right way (or far from this).
I would appreciate your help.
You are on the right track. A web service is simply a processing or storage facility intended to be accessed by some other program just like a database or fileserver service.
What is the right way to send a request to a web service
It depends. The most common implementations use SOAP or REST which define additional semantics on top of the HTTP protocol. SOAP uses a single URL as a gateway to the service and more specific selection of the functionality and the relevant data is embedded within an XML payload presented via POST. HTTP is merely a bearer for the message exchange. In REST, the HTTP part is integrated into the semantics of the transaction.
The URL identifies the location of the data (or processing function)
The payload contains only data, usually presented as POSTed JSON or XML,
further REST uses the HTTP verb (GET, POST, PUT, DELETE) to indicate the requested action
the HTTP headers are used to convey transaction meta-data.
How a web service will usually answer a request
I'm not sure what you are asking here. It should report back on the state of the request, any relevant error messages and possibly some data.
The speciifics would be unique to the API and documented.
Is cURL the right way?
While it is possible to do a lot with the HTTP wrappers functionality in PHP, libcurl offers an lot more flexibility. So, yes this it would be hard to implement a REST client without using cURL, OTOH a SOAP client is (usually) less complex at the HTTP tier but life is a lot simpler if you use a SOAP library/framework to abstract the more complex protocol.
For future questions please have one question per entry.
What is the right way to send a request to web service?
That really depends on the web service, what they require. It can be as simple as a short text string, to sending a XML formatted or JSON formatted array. You need to research the relevant web service.
How a web-service will usually answer a site requesting?
Again it depends on the web service. They are not the same. A web service will have documentation on what it expects and how it will respond.
How a PHP site can send a request to a web-service? Is cURL the right way?
Curl is a good method and is usually the method used from within PHP.
So, i've been reading a lot on SOA's lately and been trying to implement something useful. I have started with a simple blog, creating the RESTful API. So far, so good. It works perfectly. However, i'm starting to pull my hair off when writing the web interface that will consume the RESTful API. I don't know if i'm doing the right thing.
For example, the web interface has an admin panel. That admin panel makes HTTP requests to the API, through file_get_contents and stream options. Right now, the API is localhost, as well the web interface, but the whole process is a little slower. Is this right? Is this the proper way of implementing a SOA? Also, i'm dealing with little bits of duplicated code for validation. Where should i validate data? In the API or the web interface? What is the best approach?
Tips, tutorials and, specially, books are welcome. This is being implemented using Silex, built on top of Symfony components.
That's exactly how i do it. Although the connection with localhost might seem an overhead at first, it is a feature, since you're ready to deploy your web interface application anywhere and still consume your API, that might be anywhere. Of course, you would put some SSL over this.
As for Validation, you should validate on the API and return HTTP status codes for those situations (for example, "400 Bad Request" for invalid parameters). This way, any other client can interpret the response from the API and treat that to display how they want. In the case of your web interface, nice little error messages based on the HTTP status code.
What other problems are you facing? Also, as far as general SOA architecture is concerned, this book is very good.
I have some experience building web applications in html, css, javascript, jquery, and php. I understand what a wsdl document is made up of. I have been tasked to invoke a web service, which will then send an email that says "hello world."
After some hours of research, I've come across things like Apache Axis, XML-RPC, Open ESB, and Mule. Do I have to know java in order to invoke web service->send email? Is there a way to invoke a wsdl from within a web app using php?
I know about the mail() function in php. But I have been specifically told to learn how to invoke a web service. I'm lost and don't know where to begin.
I'm going to research Apache Axis until I figure out what to do.
Do I have to know java in order to invoke web service->send email?
You can create a web service using any web/server-side languange. But you should understand what is "Web Service", at the firs place, before you go too far with your question.
And stop thinking that Web Service = XML = Java.
I know about the mail() function in php. But I have been specifically
told to learn how to invoke a web service. I'm lost and don't know
where to begin.
Web services are starting to get fairly popular, but are not often well understood. When we talk about web service, we mean interfaces to data offered via HTTP, designed to be used programatically. With these loose definition, RSS feeds (also) appear to be a web service too since they share data across HTTP in format intended for parsing by software rather than humans, altough they're not usually included in such discussions.
Web service has become a label for XML sent over HTTP with read and write capabilities (but this is not the entire picturre). While HTTP always sit at the core (putting 'web' into 'webservices') we don't necessarily need to use XML, nor do we need to provide methods to write data. Many web services only allow querying and reading data.
While we already have a transport layer in our networking stack (TCP/UDP), then when we talk about web service (which actually an API on the top of HTTP), then we also need an API transport layer. When we talk about API transport layers, what we actually mean is a layer above the application layer at the top of OSI seven-layer model. Shortly, there tend to be three main contenders in this space,
A. REST
B. XML-RPC
C. SOAP
After you create your own public API, using ones of above transport layer, you can then encourage people to building with more outlandish languange and applications to build their own API kits, by providing the core set of languange support (PHP, Perl, Phyton, .NET, and Java).
So where is sending email in these explanation? Its just a part (a little one) of what you called "Web Service". After you have enough playing with "Web Service", then you will know exactly where you can put that as part of your Web Service.
I am a complete newbie in this topic so please excuse me for my ignorance. Our company has a PHP based REST service created for consumption by an iPhone app. We make several POST calls to the REST API: www.mywebsite.com/api/rest and we send the method name and the parameters to the service. The service in turn returns a XML response which is GZip encoded. This is working fine.
I am assigned with a task to secure these call so that data that is getting passed to the service is not in plain text/is not visible using tools like Charles Proxy and other sniffing tools. In the past I have used some iPhone apps which also makes REST calls but they don't show up in such tools. I would like to know how to implement such a thing in a normal PHP based REST service so the data getting transmitted is not visible/ can be made secure.
I thought about using HTTPS but if we use HTTPS, I hear from others that the service/server will become slow and I am not too sure if the iPhone client will accept HTTPS calls.
Please advice.
Thanks in advance,
Senthil
It sounds like you're trying to secure the transport layer, and assuming that's the case then SSL/TLS is surely the way to go. Yes, chances are there will be some performance impact, but the security is probably worth it.
I am new in web service. I was totally confused when seeing examples to create web service in internet. Actually, My client have xml to place the orders. what they want is they need to create a wsdl(web service) to call the xml by their vendor forms. So i need to create a webservice that integrate with xml and vendopform(it may be in any language). How i can do this, my mind has empty now to think this. Can any body help me to sort this issue.
If you're creating a SOAP service (which it sounds like you are, and I feel sorry for you ;)), try starting by looking at PHP's SOAP extension:
http://php.net/manual/en/book.soap.php
If you can create something more RESTful, Ruby on Rails does a good job at allowing you to throw together a quick and dirty web service without too many headaches, but even in PHP you can create RESTful APIs pretty easily.
You can read the HTTP request body via e.g. fgets(STDIN) if your requests will accept XML as a POST/PUT format.
But yes, your question is too broad to provide anything more than nudges towards the tools you might choose to use.