views:

722

answers:

4

Could anyone give me a small and simple example of how to do this? Or some good pointers on how to get started.

I would like to create a C# client that can send a file or some text or xml or whatever, to a web service or something similar written in PHP, where the PHP Web Service stores it in a file or a database or something like that. Just not sure how to get started.

I guess the first step would be to create the php web service. And then it perhaps would be quite easy to use it in C#, since I could probably pretty much use the "Add Web Reference" button in vs and then go from there?

+3  A: 

You can take a look at this tutorial showing how to develop a web service using php. The .NET client will be pretty straightforward as you mentioned.

Darin Dimitrov
looks interesting! thanks. will read =)
Svish
+1  A: 

I'm not versed in the C# aspect of your question, which you intend to be the consumer. But if you don't mind using a library to expedite the process of getting the webservice up and running, you can set one up very quickly using Zend Framework. Check out the documentation for setting up a Zend_Rest_Server.

chuckg
+1  A: 

Just a thought... if the service only has to support simple operations like "upload a file", perhaps avoid WSDL all together? I assume PHP can handle raw requests - so you can use the oh-so-complex .NET client:

    using (WebClient client = new WebClient())
    {
        client.UploadFile("http://some/upload.php", "foo.bar");
        client.UploadString("http://some/upload.php", "some text");
    }

etc. Likewise, you can often easily use POX / REST without the complexity of a formal web-service contract. The above code just does a simple HTTP request with the file-contents/string in the body.

Marc Gravell
that is a solution as well of course. but how would you... accept that uploaded string and file on "the other side"?
Svish
I don't know PHP, but I'm pretty sure it is fairly easy to ready an HTTP body...
Marc Gravell
A: 

you are right. all you need to do is to create a simple web service in PHP, which accepts the request/xml/file and then stores it in a database. then you can use the webservice using any technology, C# for sure. in order to write a web service in PHP, I think it is better to choose your webservice type in the first step. it can be a REST web service, or SOAP, or XML-RPC. it depends on the level of complexity of your application.

if your application is as simple as just throwing a string to the webservice or a file URL, so then the webservice can store that string, or fetch the file from the URL and stores it in database, I recomment REST. because it is simpler. as simple as just writing a simple PHP script that uses it's HTTP parameters as input.

but incase you want it to be more secure or have more complex needs, It is easy to have SOAP services in PHP. if you wanted to work with SOAP, PHP has an extension (php-soap) which you can install and use the built-in functionality of PHP-SOAP. if you are using an older version of PHP that does not support the extension, or your application will be hosted where php-soap is not installed, there is pure PHP implementaion of SOAP protocol, named nusoap. it is open source and works very well.

http://sourceforge.net/projects/nusoap/

farzad