Converting a PHP API to a C# implementation

by Damiaan Peeters 11. January 2010 13:32

I have this web service I want to use. The supplier only provides a PHP example to use the API. It is not a standard SOAP or REST service, but a custom Http POST based command.  This means that you that you either have to write

Because I had no Visual Studio installed, ready to be launched, i used the command line conversion. 

C:\Program Files\Microsoft Corporation\PHP to ASP.NET Migration Assistant>PHPConvert.exe "c:\source\PhpSource" /out "C:\source\AspDotNetVersion”

The port of the code was successful. Most of the code was converted to c#.  A lot of conversion warnings popped up, but most of them where not imposing any severe problems.

Code clean-up

A manual code clean-up is advised.  For example, this code comes clearly from an unmanaged environment:

  1. dpublic virtual void  useSecure(bool val)
  2.         {
  3.             if ((val == true) && (val.GetType() == true.GetType()))
  4.             {
  5.                 this.Secure = true;
  6.             }

Why should you check the type of “val” at runtime?  This is already done by the compiler.  A simple “ if (val== true) “  would be sufficient.  Or in this case even shorter:

  1. d        public virtual void useSecure(bool val)
  2.         {
  3.                 this.Secure = val;
  4.         }

Other things to clean up are links to magic-quotes, cURL and other specific PHP stuff.

Calling the API

Calling the API means in PHP that you do a socket write and read. For example, in PHP one would do:

$sock = fsockopen("ssl://".$this->ApiUrl["host"], 443, $errno, $errstr);

Because every effort is done to maintain the original architecture by the conversion tool.  The convertor tool added a PHP namespace containing extra support classes for duplicating the original (php) functionality. The previous line of code is thus converted to:

sock = PHP.NetworkSupport.OpenSocket("ssl://" + this.ApiUrl["host"], 443, errstr);

The OpenSocket function is a wrapper function with one line (with some additional simple error handling):

returnValue = (System.Net.HttpWebRequest)System.Net.WebRequest.Create((System.String)Target); // + ":" + System.Convert.ToInt32(Port));

Although this is neat, I like the native .Net HttpWebRequest and HttpWebResponse Classes.  So I took the code from my previous blog post: Http Post using C#, adapted it a bit and removed almost 80 lines of the converted code.

My problem with the wrapper functions created by the convertor tool, is that they use System.Object parameters and also return a System.Object.  So for readability and maintenance reasons, I started immediately converting this to some strongly typed code.


I was very pleased with this tool.  I saved literally hours typing, and it took me (only) a few hours of refactoring.   Refactoring meant:

  • removing unused functionality (magicQuotes, cURL, …)
  • removing unnecessary type checks using Relfection
  • removing calls to wrapper functions when I had decent managed .Net code available in my library

And just now I started thinking that it may have been faster implementing a custom WCF implementation. 

Taggedmail profile

by Damiaan Peeters 4. January 2010 15:29

This is weird.  Yesterday my traffic on my side got some new traffic.  The website stats showed me (after a little bit of digging), the following figures.


Traffic with all the same keyword: “taggedmail profile”.  There were also some visits for the “tagged mail profile”, but much less.  The traffic initiated from different countries, but the majority came from Portugal.  I normally never get traffic from Portugal.    All traffic landed on this page:  It’s true, the tag “mail profile” is there.  But I doubt it was what they where searching.

I don’t know what it means.  I search Google, I searched Bing.  But couldn’t find any reasonable explanation. My guesses are:

  • spam bots searching for user profiles for email addresses. 
  • some company takeover of which i am not (yet) aware
  • someone interested in buying (currently pointing to

After these ideas came up, I started searching TechMeme, Mashable and TechCrunch.  But none of them supplied me with some answers, expect for the CSE from Techcrunch: How do I get rid of tagged spam from Maybe some spam was send out from the domain taggedmail (.com, .net, …).  But what with the profile keyword?

If someone has any idea, I’ll read it in the comments.

Certified Umbraco, .net and Azure developer, seo lover. Magician in my spare time.

Month List