Send a file to a PHP script from a VB.NET app
I need to send some data from a SQL DB Server on an internal network to an external web server.
I was hoping to accomplish this开发者_开发知识库 by writing a VB.NET app that is invoked once per day that sends about 1 MB of data to a PHP script on the web server, where it is deposited in a database.
What is a good method to send data to a PHP script from a .NET app?
You can use the HTTPWebRequest class to post a file to your php script just as a browser would. If the file you are sending is text based like XML or CSV, a simple post could be done. However, if you need to send binary data, you could either encode it base64 and then decode it on the other end in PHP, or post it as a file, using multipart forms. Unless you have a specific reason to be using multipart forms, it probably isn't worth all the extra trouble, and you should probably just encode your data with base64.
Web services would be the way to go, perhaps SOAP via XML to push the data directly to PHP.
If you go middaparka's route, you're not pushing it directly to PHP, but rather just making it available to PHP using an include statement. If you want PHP to process the data immediately when it's received, you'll probably need the first option.
If you're after a reliable means, I'd be tempted to use FTP as a means of data transfer rather than HTTP - it'll make things a lot easier to manage in terms of error handling, etc. especially if you follow some good practices such as uploading the files into a temporary folder and then moving (renaming in FTP terms) them to a pickup folder once the upload is complete.
In terms of specific functions, etc. PHP has a wide range of FTP related functions (key docs here). I'm unfortunately not aware of the relevant .NET framework bits and bobs, but I'm sure someone here will point you in the right direction if MSDN doesn't deliver the goods.
You might want to use JSON or XML.
Another idea could be to execute a HTTP PUT request to PHP based website and transfer your data.
Find here how to accept content sent through HTTP Put in PHP http://php.net/manual/en/features.file-upload.put-method.php
See here how you can execute HTTP PUT request from .net Application http://www.java2s.com/Code/CSharp/Network/HTTPputwithusernameandpassword.htm
精彩评论