Skythrust
Member
- Jul 9, 2019
- 133
- 7
Goodmorning people,
I have a web page with some PHP data (JSON) and I would like to read this string in C#.
PHP string; [{"Firstname":"Janet","Middlename":"van","Lastname":"Hope","Key":"12018","Cid":5}]
This string will change when there is a new record for Cid:5 (SELECT TOP 1 * FROM Users WHERE Cid = 5 ORDER BY CreatedOn)
Now I am trying to make a Windows Service which is reading this array, I was thinking to do it like this with a read command
string Firstname = "";
string Middlename = "";
string Lastname = "";
string Key = "";
string Cid = "";
using(WebClient client = new WebClient()) {
string s = client.DownloadString(url);
}
But I have no idea how I can call the right parameters (Firstname,Middlename,Lastname,Key,Cid)
The next step in this call is to insert them in a sqlsrv_connect, but I have no idea how to do that.
Extra Info; I have 2 seperate servers, in a datacenter (1 applicationserver with IIS & SQL with the PHP string, and another one with an other application.) port 1433 (SQL) is closed and cannot be opened for security reason.
There is no LAN connection between those and the future will be is that anyone can install the application (service) to connect application A with application B without portforwarding and just reading the PHP string. Over here they are talking that I need to call it a SOAP intergration.
But I am not familliar with this. So I need to call the PHP string from server A in a service to push it in a insert via a Windows service for server B. I can insert the PHP code but this seems okay I guess.
If there is more information needed let me know.
I would like to use this code in a service, but I am not able to do that
when I paste this code in a Winform I am able to recieve the information, and I am able to insert it into a SQLSRV.
I have a web page with some PHP data (JSON) and I would like to read this string in C#.
PHP string; [{"Firstname":"Janet","Middlename":"van","Lastname":"Hope","Key":"12018","Cid":5}]
This string will change when there is a new record for Cid:5 (SELECT TOP 1 * FROM Users WHERE Cid = 5 ORDER BY CreatedOn)
Now I am trying to make a Windows Service which is reading this array, I was thinking to do it like this with a read command
string Firstname = "";
string Middlename = "";
string Lastname = "";
string Key = "";
string Cid = "";
using(WebClient client = new WebClient()) {
string s = client.DownloadString(url);
}
But I have no idea how I can call the right parameters (Firstname,Middlename,Lastname,Key,Cid)
The next step in this call is to insert them in a sqlsrv_connect, but I have no idea how to do that.
Extra Info; I have 2 seperate servers, in a datacenter (1 applicationserver with IIS & SQL with the PHP string, and another one with an other application.) port 1433 (SQL) is closed and cannot be opened for security reason.
There is no LAN connection between those and the future will be is that anyone can install the application (service) to connect application A with application B without portforwarding and just reading the PHP string. Over here they are talking that I need to call it a SOAP intergration.
But I am not familliar with this. So I need to call the PHP string from server A in a service to push it in a insert via a Windows service for server B. I can insert the PHP code but this seems okay I guess.
If there is more information needed let me know.
I would like to use this code in a service, but I am not able to do that
Code:
using (var webClient = new WebClient())
{
string json = webClient.DownloadString(new Uri("http://localhost/test.php"));
var Personal = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Object>>(json);
foreach (var pv in Personal)
{
string Badge = pv.CredentialNumber;
string Firstname = pv.Firstname;
string Middlename = pv.Middlename;
string Lastname = pv.Lastname;
SqlConnection sc = new SqlConnection(GetConnectionStringTemp());
{
sc.Open();
string query = string.Format("INSERT INTO Visitors (Badge,Firstname,Middlename,Lastname) VALUES ('{0}','{1}','{2}','{3}')"
, Badge //0
, Firstname //1
, Middlename //2
, Lastname //3
);
using (SqlCommand command = new SqlCommand(query, sc))
{
int countrecords = command.ExecuteNonQuery();
}
sc.Close();
}
}
}
when I paste this code in a Winform I am able to recieve the information, and I am able to insert it into a SQLSRV.
I have done some more research and I got this now;
C#:
void JSONtest()
{
Uri requestUri = new Uri("http://localhost/test.php");
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUri);
request.Method = "GET";
request.BeginGetResponse(new AsyncCallback(ProcessResponse), request);
}
private void ProcessResponse(IAsyncResult asynchronousResult)
{
string ExportFromPHP = string.Empty;
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
using (HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult))
{
Stream responseStream = response.GetResponseStream();
using (var reader = new StreamReader(responseStream))
{
ExportFromPHP = reader.ReadToEnd();
}
responseStream.Close();
}
// Do something?
MessageBox.Show(ExportFromPHP);
}
}
Last edited: