I am creating a PHP website connected to a MySQL database. Next I will need to write a C# desktop app that will use the same DB. Unfortunately I cannot connect to the DB directly from a remote location and my hosting company won't allow SSH neither.
So what options do I have? If the hosting company supported .NET, it wouldn't be a problem, but I'm not that experienced with PHP. Will I have to write a PHP service (SOAP?) and then consume it in my desktop app? Also, how do I communicate with server from the desktop app?
Any help appreciated!
Depending on security requirement, could you write a generic SQL executing page in PHP, that took the SQL as a String parameter, and returned the results as an array of Strings (Might need some meta data too or something)?
Other than that the only thing I can think of is a web service of some kind.
Also SOAP can work both ways, you can read and write from the C# app, no need to write a WebService on both ends, unless you need to notify your c# app about something from the server (In which case you could always try frequent polling from the c# app)
Best option would be creating a set of RESTful services in your PHP site.
One of most important things to take in account is REST is more configuration by convention, and there's no need of things like SOAP which may be an absolute overkill for your solution.
You just send JSON from PHP and .NET Windows application will parse it as a CLR object.
A sample scenario would be:
Service operation: http://yourdomainhere.com/API/Message/34894
** This returns something like { "text": "hello world" }
.NET client receives this JSON and using a JSON parser like Newton JSON parser, you'd be doing this:
MessageDto dto = JsonConvert.DeserializeObject([JSON received from the service call]);
MessageBox.Show(dto.Text); // This will show "hello world"
It's just a very simple example, but it'd give you an idea of what's next.
You can query your REST API using WebRequest/WebResponse .NET BCL classes.
PHP only needs to send a web response including your JSON in the output stream, that's all. No SOAP, no XML, no complication. Keep it simple.
I think the following link will be of helpful to you!
Developing SOAP Web Services with PHP/C#
What you can do is providing some PHP-wrappers which you can access from your C# code. As an example you can use this discussion, regarding C# / PHP communication.
Basically you can send a HTTP request to PHP and retrieve it's return value with C#. PHP would then perform the DB requests. If you're using AJAX on the Website it should be easy using the same communication interfaces.
this is the first paragraph of Matt Fellows answer.
But in what form do you send the data back to the application in?
Maybe JSON?
PHP webpage
<?php
$host = "host.host.com";
$user = "XXXXX";
$password = "XXXX";//plaintext :)
$connection = mysql_connect($host, $user, $password);
$database = "XXXXX";
$syntax = $_GET['syntax']; //www.example.com/help.php?syntax=DROP%20TABLE%20XXX
$result = mysql_query($syntax);
//somehow output the $result in C# readable form
?>
Related
So I was thinking about learning about app development for android. I know you use kotlin however, I also want to start working with .NET and C# is there a possibility for my first app that I create a basic login and register form in the app using Kotlin and connect it to a .NET REST API? Is that a thing I am sure you are just using the URL for the API call?
Sure! Using Retrofit, the Android app could be connected to the RESTful APIs that is available using the latest technology by Microsoft and the open source community; ASP.NET Core Web API 5.
A complete guide to do so:
http://codingsonata.com/a-complete-tutorial-to-connect-android-with-asp-net-core-web-api/
yes it is possible, there are plenty of libraries that are able to help you with that, like Retrofit or Volley
Yes. It's a very common way to consume API for data processing purposes in android-based applications.
If you have learnt on how to consume API with Android Application (built using JAVA), it's pretty much the same.
if you have never used JAVA to create android applications that consume API, don't worry because the process you need to do is very simple, moreover there are already many collections of libraries that you can use for this purpose. You can try to look on several library such as Retrofit and OkHttp.
Let me show you a a simple example of using OkHttp to pass data from android to API. This simple example is quoted from this article
private val client = OkHttpClient()
fun run() {
val formBody = FormBody.Builder()
.add("search", "Jurassic Park") /*The parameters*/
.build()
val request = Request.Builder()
.url("https://en.wikipedia.org/w/index.php") /*API URL*/
.post(formBody)
.build()
client.newCall(request).execute().use { response ->
if (!response.isSuccessful) throw IOException("Unexpected code $response")
println(response.body!!.string())
}
}
I hope this answer can be helpful for you. If you need more assistance, don't hesitate to contact.
If you are using API, you use URL and model of your data.
I recommend Retrofit as a library for connecting to any API. A good example is here, check out this and I think you will be well prepared to write Kotlin code.
Firstly, you need to define retrofit client, with your URL:
retrofit = new Retrofit.Builder()
.baseUrl("https://reqres.in")
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();
then you need to map your API as an interface, for example if you have an endpoint https://reqres.in/api/users? you must define them like this:
#GET("/api/users?")
Call<UserList> doGetUserList(#Query("page") String page);
For creating .NET Core API, especially with user registration you must check some of tutorials, I recommend to be familiar with EF Identity (other).
I have recently returned to .net programming after a 7 year break.
I need to learn how to write a project within an existing open source asp.net mvc 5 ecommerce solution to receive posted json strings from a remote server running php with cURL, send acknowledgement responses, create my own json strings to post back to the remote server and receive acknowledgement responses. This must all be done with server side code, with no client side component whatsoever.
Serializing and deserializing json is not the issue, its using the correct kind of pages or services to send and receive json on the server without any client component, and using http objects directly. I have no experience or knowledge of creating this kind of project.
This is my question: I have had a look at a couple of tutorials about using .ashx and httpClient and httpContext but found them a little confusing. I would like to find a comprehensive guide about how to use json to communicate server to server with realistic examples. Is there one available?
Sounds like a perfect use case for WebApi. It is made specifically to work with JSON (or XML) requests and should work fine with requests issued by other scripts (not browsers).
There are plenty of tutorials available. Here is the official introduction tutorial.
What I often do to create JSON in C# is make an object/class which I serialize to JSON.
My controller has an function:
public JsonResult FunctionName()
{
var json = new { x1 = 10, y1 = "Hello" };
return Json(json);
}
You can call that function with PHP.
I got one question. I am going to use RESTfull web service in my mobile app(which is based on C#, monotouch). I have to send big data using either json or xml. Can I make sure that in case connection is lost I want to stop pulling data and cancel rest of the data which has been pulled.
Basically. my target is that I don't want to pull partially. I need whole data without loosing it.
Any other idea how can I achieve this?
Just wanted to ask another question. How can I secure my url which refer to data. If I use RESTfull webservice
If you use NSURLConnection as follows
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:[NSMutableURLRequest requestWithURL:[NSURL URLWithString:#"http://www.google.com"]] delegate:self];
Then, you can use the following two delegate methods to check if the data downloaded fully.
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
//Download failed
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
//Download success
}
Its going to a grt puzzle for me now a days. I have developed a product which is implemented using multiple languages such as a C# Windows app, Titanium iOS app, and a Java application with a team of friends.
I am using a c# web service which is taking parameter of datatype byte[]. I have completed my work on windows app by adding it in service reference.
My Titanium team mate asked me to create some sample code for this web service without using service refrence directly by the url, but instead, either:
Call it with soap or http post methods.
Create a web sevice that they will use with titanium in a easy way
Any other useful idea on how to use the same webservice with titanium
As titanium boy is fresher with titanium right now so I have to do something but I am also stuck and don't know how to suggest him something so I need help from your side.
I suggest you encode your binary data into a Base64 string and send it as such to your C# service. Since you're using SOAP, it would be a very simple solution.
Just use the built in Titanium utilities to encode your data to base64:
// Encode your data
var data = Titanium.Utils.base64encode(dataToSendToWebService);
Now send it using a HTTPClient:
var postDataTOServer = Ti.Network.createHTTPClient({
onload : function(e) {
// If the service returns successfully : true, or false
var isUserAllowed = this.responseText;
},
onerror : function(e) {
// Web service failed for some reason
Ti.API.info(this.responseText);
Ti.API.info('webservice failed with message : ' + e.error);
}
});
// POST
postDataTOServer.open('POST', 'http://yoursite.com/aspwebservice');
// you may have to change the content type depending on your service
// but this is the correct type for binary data
postDataTOServer.setRequestHeader("Content-Type", "application/octet-stream");
// This does a POST to server
postDataTOServer.send(data);
I am new to developing with the Goolge API’s. I am trying to get the Google.Apis.Freebase.V1 API working in C#. Does anyone have a small example on using this API in C#? I have spent the last several days looking and can only find a couple of examples for the old Freebase Api. Nothing for the Google API.
I am just looking for a simple example on setting up a connection to the API, doing a search, then how to handle a MQL query back into a Json object. The simpler the better.
Thanks
Scott
The correct code to do a MQL query in C# using the Google API Client Library should look something like this:
string API_KEY = "your-api-key-here";
FreebaseService service = new FreebaseService{ Key = API_KEY };
String query = "[{\"id\":null,\"name\":null,\"type\":\"/astronomy/planet\"}]";
FreebaseService.MqlreadRequest request = service.Mqlread(query);
string response = request.Fetch();
Console.WriteLine (response);
Unfortunately, there seems to be some sort of error with the client library right now as its not returning any results. I'll try to figure out what's going on there.
Update: The problem appears to be that the client library passes along an alt=json parameter which the Freebase API is unable to support. The Python client library has a way to disable this but there no way to do it in .Net. You can follow the open bug for this on the Google Code project.