Developing a Web Service (Server)

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

Developing a Web Service (Server)

Post by kendall »

Hello,

I have read many articles concerning using WebServices but all of them was focused on using SOAP and from a "client" side perspective. I am trying to create a Webservice "Server" But I'm not sure how to conceptualize this aspect. What i know is that I am going to be "recieving" RAW XML output content which i have to capture some how and parse inorder to process the information recieved. But how do i do that?

Now i have been researching a PHP defined variabled $HTTP_RAW_POST_DATA but i am getting mixed thoeries on whats its used for and how to "access" it. But Is this the approach that is adopted when developing for this nature?

Can someone direct me to any articles or tutorials that explore the "server" development side of Web Sevices? I am interested in finding out about the expectations and best practices when developing this sort of thing can have alot of concerns as to providing a "safe" and trusted environment. As well as ensuring proper execution of the process...

Kendall
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Developing a Web Service (Server)

Post by John Cartwright »

Requests are either POST or GET.. these requests are usually done using the POST method, which is available in the $_POST superglobal.

Really good article about HTTP requests and XML, http://skew.org/xml/misc/xml_vs_http/
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

Re: Developing a Web Service (Server)

Post by kendall »

Jcart,

THANKS a MILL man...this is exactly what I was looking for. Quick question I'm hoping you can elaborate on for me
Since POST is a good method for transmitting documents, the document must be sent not in the request URI, but in the body of the request message. The body follows a blank line after the headers, just like in an email message. The headers must contain a Content-Length header indicating the number of octets (8-bit bytes) contained in the body. A Content-Type header is required. The charset parameter must match the actual encoding of the document.

An example of an ideal, but impractical, HTTP message for XML entity transmission follows.

POST /MessageReceiver.jsp HTTP/1.0
Host: http://www.SomeHost.net
Content-Type: application/xml; charset=iso-8859-1
Content-Length: 68

<?xml version="1.0" encoding="iso-8859-1"?>
<doc>hello world</doc>

...But all HTTP implementations do not handle POST data well
On the server, MessageReceiver.jsp must be prepared to accept the complete message body as the XML entity. Java Servlet request objects have a getInputStream() method that allows access to the raw message body, but this method is relatively unknown and underutilized. And other server-side interfaces such as Perl's CGI module do not offer such an option at all.
This is exactly what I am being faced with. I'm recieving response information that is wrapped up in the xml document being transmitted back to me...Like the Java Servlet I need a PHP object that would allow me to access the raw message...? Whats the best implementation of this...

also...how do i check for such things as where the source came from and authenticating that the source came from where it suppose to come from?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Developing a Web Service (Server)

Post by John Cartwright »

I've never seen such a situation, so you cannot retreive the document normally using $_POST['xml']?.

maybe..

Code: Select all

$headers = get_headers();
echo '<pre>';
print_r($headers);
 
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

Re: Developing a Web Service (Server)

Post by kendall »

Not as far as indicated...everything is wrapped in xml...


the information is wrapped in the body of the content being transmitted so i'm not sure if get_headers will work....but i'm experimenting at the moment...will let you know
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Developing a Web Service (Server)

Post by John Cartwright »

Oh okay I understand now. I actually never knew about $HTTP_RAW_POST_DATA. Supposedly you need always_populate_raw_post_data enabled in your php.ini for it to exist.

However, you can use php://input to capture the input stream.
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

Re: Developing a Web Service (Server)

Post by kendall »

yeah....seems that way...never knew about that variable...I;m trying to to run some test but the trouble is to try to simulate an environment for it....thanks for the help though that article u gave me did wonders :wink:
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

Re: Developing a Web Service (Server)

Post by kendall »

Well I tried using $HTTP_RAW_POST_DATA and apparantly i need to access it via...

Code: Select all

$_GLOBALS['HTTP_RAW_POST_DATA']
using it in conjunction with

Code: Select all

file_get_contents('php://input');
is recommended
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Developing a Web Service (Server)

Post by John Cartwright »

I would never rely on a server configuration setting for something to exist.

//register globals walks in room.. *cough*
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

Re: Developing a Web Service (Server)

Post by kendall »

Is there any way to check to see if the response is coming from the reliable source url that suppose to be accessing the service? :?: I'm looking at the headers being posted and i'm thinking...."would that be reliable in the first place" :?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Developing a Web Service (Server)

Post by John Cartwright »

Not without help. Typically services require some kind of authorization key to be sent along in the request. If the key is missing/wrong the request is ignored.
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

Re: Developing a Web Service (Server)

Post by kendall »

ohh ok...yeah well there is a key just thought i cud have stopped it from the actual transmission. i find it becoming fairly simple now...thanks for that article.
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Re: Developing a Web Service (Server)

Post by Maugrim_The_Reaper »

Are you sure you're using the right approach? Sounds like you are taking an RPC route with incoming requests containing XML, instead of the simpler (usually) REST based approach based on queries forming a URL sent via GET or POST.

Valid incoming requests generally require some form of API Key unique to a client.

The contents of the XML are like any other user input. Assume suspect until proven otherwise using validation/filtering. Same as for a typical form submit ;).
Post Reply