How can I transfer a PHP array via XML? - c#

I have an XML document that is encrypted using a proprietary encryption program like so:
exec($programName, $outputArr, $returnVal);
The resulting array ($outputArr) was serialized and stored as a blob in MySQL. I was making this encrypted array available on demand through a web interface. Basically deserializing and writing to a file on the fly.
Now the requirements have changed and I need to make this encrypted array available to another server which uses .net/c# and which in turn will host the web interface. We are using a REST API that responds to a GET request and sends out an XML response.
I tried writing the array into a temporary file and then retrieving the contents of the file using file_get_contents($tempFile) and then URL encoding the resulting string and putting that inside of the XML response that I was sending.
Of course, when the new web interface writes this out as a file (after URL decoding), it's nothing like it's supposed to be. By that, I mean our proprietary program throws up errors on reading this new resulting file ... somewhere along the way, there's data corruption happening.
We also tried a C# serialization library that deserialized PHP arrays into a c# primitive type but that wasn't a good solution either as it kept throwing up a bunch of errors.
Is there a better way to do this?

Why not just access the working version, and re-manipulate it before sending to the new server...
<!-- Some XML based wrapper or other logic/output -->
<?php
echo file_get_contents($url_to_working_web_interface);
?>

Related

Parse stdClass object in C#

I'm currently working on a client that has more features than another existing client for a game. One of the functionalities I would like to add is the ability for a user to add an IP address to their favourites and display relevant information of that server (IP, Port, Hostname, Gamemode etc.).
In order to do this I use an API which takes several parameters, such as the IP address and port. It returns some kind of string which contains the information that I need. However I'm having trouble extracting the information I want from the string. I did manage to do it, but I did it quick and dirty by using a dozen substrings to cut out the values of the parameters from the response.
Here's an example of an API call: http://monitor.sacnr.com/api/?IP=149.202.89.123&Port=7777&Action=info
Initially I thought the response was some kind of JSON string, but after some googling I found a question on Stackoverflow about stdClass. Apparently it's some kind of generic PHP class.
So my question is: how can I convert the response of the API to say JSON or XML, so I can easily parse it?
Example response:
O:8:"stdClass":18:{s:8:"ServerID";s:7:"1780392";s:2:"IP";s:14:"149.202.89.123";s:4:"Port";s:4:"7777";s:8:"Hostname";s:32:"Next Generation Roleplay (0.3.7)";s:8:"Gamemode";s:14:"NG:RP v3.0.409";s:8:"Language";s:7:"English";s:3:"Map";s:13:"NG-Gaming.net";s:10:"MaxPlayers";s:3:"500";s:7:"Players";s:3:"131";s:7:"Version";s:8:"0.3.7-R2";s:8:"Password";s:1:"0";s:4:"Time";s:5:"20:00";s:6:"WebURL";s:13:"ng-gaming.net";s:4:"Rank";s:3:"112";s:10:"AvgPlayers";s:5:"88.27";s:9:"HostedTab";s:1:"1";s:10:"LastUpdate";s:10:"1486138780";s:12:"TotalServers";s:4:"1305";}
Try using the Sharp Serialization Library.
As per their description: "Sharp Serialization Library serializes and deserializes primitives, ArrayLists and Hashtables, compatible with PHP serialize()"

Read contents JSON file sitting on webserver from c# code behind

I am trying to read the contents of a JSON file sitting in my github pages repository.
I can navigate and see the file contents in my browser if I specify the url.
If I use the code here:
http://www.codeproject.com/Tips/397574/Use-Csharp-to-get-JSON-Data-from-the-Web-and-Map-i?msg=4615047#xx4615047xx
It claims to "just work", but it doesn't.
All I get back is:
<html><frameset><frame src="URL-TO-JSON-FILE"></frameset></html>
How am I supposed to read the json file and get its contents back as a string. I am using c#?
Once I get the JSON string back I can do the processing I need to do in c#.
EDIT:
According to rawgithub.com those types of urls are not to be used for production. I need this for production. How do production website read remote JSON files that are located on a webserver?
Thank you
Sometimes in github, if you wish to use code from a repository, you must change the url to raw.github.com/ or click on the raw button and use this url.

How to extract multipart attachments from SOAP response

I am trying to extract the multipart attachment portion of a SOAP response.
I have a project that uses a Web Reference to talk to a 3rd party web service. I'm able to successfully make requests and get valid responses back from the service, but I don't understand where if anywhere the attachment data is going. Looking through the Web Reference autogenerated code there aren't any objects that match up to the actual attachment data.
Are there any kind options that I need to set when originally consuming the 3rd party WSDL to make this work correctly?
I had hoped there would be a .NET multipart boundary parser already, but wasn't able to find one so I wrote my own.
I have done this by getting the raw HTTP response as a string, splitting on the part boundaries ------=_Part_*. Then for each part looking for Content-Transfer-Encoding: base64, and then extracting the base 64 region and using Convert.FromBase64String to convert that to a byte array, and finally saving that as a file.

Server side PHP sql queries to C# app

I'm retrieving some userdata from a phpbb3 forum through scripts on the server.
My database does not allow for external login, so I have to go through some kind of script.
But, I'd like to know if there is a safe enough way to retrieve this data?
I know how to output some XML with echo statements, but I'm not sure if this is as secure as it should be?
Optimal method would be (psuedo code)
$array['user_id'] = $id;
$array['otherinfo'] = $var
return $array;
if I could somehow read this array with C#, it would be much easier, but I'm not sure how this would work with WebResonses or whatnot.
Any ideas?
You need a web service, that will output your responses in some format that you can parse with C#.
Php and C# are not interoperable, that s why you will need to create a service that you can consume these messages.
First option as I said is to create a web service, that C# code can consume. In this case, your C# code should know what to ask from your php web service, and you will respond to the request, in XML, or JSON or whatever format you want to use within your C# code to parse it.
Another option is to push the data to a web service that uses C#. You need to write a WCF/ Web service, and you can push the data to this service.
I suggest you to give your array output as xml or json. If we take xml as consideration, you can call the php file which gives xml output like this:
XDocument.Load("http://whatever.com/whatever.php");
And ofc you need to add the System.Xml.Linq header to use XDocument class. And also you can pass a hashed key, etc. from query string to validate the requested is coming from your app.

How to use System.Net.WebClient() to download XML from a webservice with correctly encoded tags?

I'm using System.Net.WebClient().DownloadFile() to download the response of a webservice to a file. The response is rather large, so I don't use direct SOAP calls which would return a single string, eating up too much memory.
The response of the webservice is XML and what I get back is something like
<?xml version="1.0" encoding="utf-8?>
<string xmlns="MyAPI>
<object name="First object">
...
So the issue is that the whole response gets wrapped into a "string" attribute and the real XML content gets encoded.
Without re-reading the whole saved file and converting everything back, how can I get the response in its original and correct format?
I don't know why you believe that a web service call would take up too much memory. Did you try WCF?
Your problem is that you are calling the service the wrong way. You are no doubt using a GET HTTP request.
If you insist on not using a Service Reference, then I recommend you use WebRequest, and read the result through the GetResultStream() method. You could even process the result as XML by passing that stream into XmlReader.Create(Stream), which will return an XmlReader object that can be used to read the data without reading it all into memory.

Categories

Resources