Menu
Forums
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Trending
What's new
New posts
New profile posts
Latest activity
Members
Current visitors
New profile posts
Search profile posts
Upgrades
Log in
Register
What's new
Search
Search
Search titles only
By:
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Menu
Log in
Register
Navigation
Install the app
Install
More options
Contact us
Close Menu
Forums
Software Development
Programming
Programming Q&A
C#/PHP read JSON string from PHP into C#
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="Skythrust" data-source="post: 456349" data-attributes="member: 87030"><p>Goodmorning people,</p><p></p><p>I have a web page with some PHP data (JSON) and I would like to read this string in C#.</p><p></p><p>PHP string; <strong>[{"Firstname":"Janet","Middlename":"van","Lastname":"Hope","Key":"12018","Cid":5}] </strong></p><p></p><p>This string will change when there is a new record for Cid:5 (SELECT TOP 1 * FROM Users WHERE Cid = 5 ORDER BY CreatedOn)</p><p></p><p>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</p><p></p><p>string Firstname = "";</p><p>string Middlename = "";</p><p>string Lastname = "";</p><p>string Key = "";</p><p>string Cid = "";</p><p></p><p></p><p>using(WebClient client = new WebClient()) {</p><p>string s = client.DownloadString(url);</p><p>}</p><p></p><p>But I have no idea how I can call the right parameters (Firstname,Middlename,Lastname,Key,Cid)</p><p></p><p>The next step in this call is to insert them in a sqlsrv_connect, but I have no idea how to do that.</p><p></p><p>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.</p><p>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.</p><p>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.</p><p></p><p>If there is more information needed let me know.</p><p></p><p>I would like to use this code in a service, but I am not able to do that</p><p>[CODE]</p><p>using (var webClient = new WebClient())</p><p> {</p><p> string json = webClient.DownloadString(new Uri("http://localhost/test.php"));</p><p> var Personal = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Object>>(json);</p><p></p><p> foreach (var pv in Personal)</p><p> {</p><p> string Badge = pv.CredentialNumber;</p><p> string Firstname = pv.Firstname;</p><p> string Middlename = pv.Middlename;</p><p> string Lastname = pv.Lastname;</p><p></p><p> SqlConnection sc = new SqlConnection(GetConnectionStringTemp());</p><p> {</p><p> sc.Open();</p><p></p><p> string query = string.Format("INSERT INTO Visitors (Badge,Firstname,Middlename,Lastname) VALUES ('{0}','{1}','{2}','{3}')"</p><p></p><p> , Badge //0</p><p> , Firstname //1</p><p> , Middlename //2</p><p> , Lastname //3</p><p> );</p><p></p><p> using (SqlCommand command = new SqlCommand(query, sc))</p><p> {</p><p> int countrecords = command.ExecuteNonQuery();</p><p> }</p><p> sc.Close();</p><p> }</p><p> }</p><p> }</p><p>[/CODE]</p><p></p><p>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.</p><p></p><p>[SPOILER= Already Solved]</p><p>I have done some more research and I got this now;</p><p></p><p></p><p></p><p>[CODE=csharp]</p><p></p><p>void JSONtest()</p><p></p><p> {</p><p></p><p> Uri requestUri = new Uri("http://localhost/test.php");</p><p></p><p> HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUri);</p><p></p><p> request.Method = "GET";</p><p></p><p> request.BeginGetResponse(new AsyncCallback(ProcessResponse), request);</p><p></p><p> }</p><p></p><p></p><p></p><p>private void ProcessResponse(IAsyncResult asynchronousResult)</p><p></p><p> {</p><p></p><p> string ExportFromPHP = string.Empty;</p><p></p><p> {</p><p></p><p> HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;</p><p></p><p> using (HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult))</p><p></p><p> {</p><p></p><p> Stream responseStream = response.GetResponseStream();</p><p></p><p> using (var reader = new StreamReader(responseStream))</p><p></p><p> {</p><p></p><p> ExportFromPHP = reader.ReadToEnd();</p><p></p><p> }</p><p></p><p> responseStream.Close();</p><p></p><p> }</p><p></p><p> // Do something?</p><p></p><p> MessageBox.Show(ExportFromPHP);</p><p></p><p> }</p><p></p><p> } [/CODE]</p><p>[/SPOILER]</p></blockquote><p></p>
[QUOTE="Skythrust, post: 456349, member: 87030"] Goodmorning people, I have a web page with some PHP data (JSON) and I would like to read this string in C#. PHP string; [B][{"Firstname":"Janet","Middlename":"van","Lastname":"Hope","Key":"12018","Cid":5}] [/B] 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(); } } } [/CODE] 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. [SPOILER= Already Solved] I have done some more research and I got this now; [CODE=csharp] 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); } } [/CODE] [/SPOILER] [/QUOTE]
Insert quotes…
Verification
Post reply
Forums
Software Development
Programming
Programming Q&A
C#/PHP read JSON string from PHP into C#
Top