18

I need this PHP code converted to C#. Is there a tool or a website that would make this possible?

public function call($method, array $params) {
    // Add the format parameter, only 'json' is supported at the moment
    if (!array_key_exists('format', $params)) {
        $params['format'] = 'json';
    }

    $url = "{$this->_url}/{$method}";
    $ch = $this->_getCurlHandle($url);

    if (!$ch) {
        throw new Fuze_Client_Exception("Unable to create a cURL handle");
    }

    // Set the request parameters
    $queryString = http_build_query($params);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $queryString);

    // Fire!
    $result = $this->_executeCurl($ch);

    // All API response payloads should be valid json with 'code' and
    // 'message' members
    $json = json_decode($result);
    if ( !($json instanceof stdClass)
            || !isset($json->code)
            || !isset($json->message) ) {

        throw new Fuze_Client_ServerException(
            "Invalid JSON payload received", $result, $info['http_code']);
    }

    if ( $json->code >= 500 && $json->code < 600) {
        throw new Fuze_Client_FaultException($json);
    }

    return $json;
}
mingos
  • 23,778
  • 12
  • 70
  • 107
SOF User
  • 7,590
  • 22
  • 75
  • 121
  • This seems to be part of something larger. What aspect of it do you need translated exactly? The curl part? – Pekka Mar 07 '11 at 15:45
  • Related: http://stackoverflow.com/questions/441161/how-to-convert-code-from-c-to-php – RQDQ Mar 07 '11 at 15:45
  • Might not be what you're looking for, but Facebooks HipHop for PHP converts PHP to C++. (Yes, I know you're asking for C#, but this is the closest thing I can think of:D). Doesn't really get you any converted code, but increases your performance if that's what you're after. – Sondre Mar 07 '11 at 15:55
  • if it is just for those few lines of the code above... the old-school manual "hand-made" conversion should be the easiest and quickest way, I think... for several kloc think about using phalanger (as I already mentioned below). If this does not work try to use interop instead of conversion (e.g. bridge the calls through a service). – Beachwalker Nov 16 '12 at 11:56
  • check this site... its work... just you need chang a part of class and names... https://react-etc.net/entry/peachpie-online-repl-transpiles-php-to-net-core-compatible-c-sharp – Amirhossein May 07 '19 at 18:58

7 Answers7

42

I don't know of any tool that can convert from PHP to C#. If there is such a thing, I wouldn't trust it to do the conversion correctly. So that leaves your options to:

  • Learn C#
  • Pass it to someone who can convert it.
RQDQ
  • 15,461
  • 2
  • 32
  • 59
  • 3
    +1, seconded. Converting source code from one language to another is a complex task, I doubt there's a tool that would make it possible, not to mention reliability :). – mingos Mar 07 '11 at 15:50
  • 4
    Instead of conversion, it is possible to integrate existing PHP code into .NET, e.g. using Phalanger as mentioned at http://stackoverflow.com/questions/5221669/php-to-c-sharp-converter/8007020#8007020 It is being used – Jakub Míšek Mar 28 '12 at 11:53
  • @JakubMíšek and then reverse engineer that hah. actually, converting simple blocks must not be too hard, it's just no one considered it worthy to address – jungle_mole Sep 11 '15 at 12:19
  • 1
    Please note "Converting source code" is called "compilation". There is nothing wrong with it. You can compile F#, C++/CLI, TypeScript and use it from C# pretty straightforwardly. Why not compiling PHP? Use either Phalanger mentioned above or https://github.com/iolevel/peachpie – Jakub Míšek Feb 17 '17 at 14:33
19

Maybe you should take a look at Phalanger. It is not a converter, but it allows you compile PHP code for .Net - so you should be able to call php methods from C#. It is an open source project and the source is hosted here.

Beachwalker
  • 7,685
  • 6
  • 52
  • 94
7

Instead of converting (which would lead to an unmanageable spaghetti code), make the PHP project a regular .NET project (like F# or C++/CLI) using PHP compiler we're working on:

https://github.com/iolevel/peachpie

In this way, beside other things, you keep your PHP code maintainable while you can seamlessly use it from C#.

Some resources:

Jakub Míšek
  • 1,036
  • 9
  • 12
4

There is no converter, but You may take a look at Haxe. It's multi-platform open source language that can be compiled into other languages, including C#. The syntax is similar to PHP.

Anyhow, for this code snippet converting manually would be best approach.

4

This might be helpful. It has a conversion engine but the fact it is supported with 'whitepapers' leads me to believe that there will still be hard yards ahead.

http://www.asp.net/downloads/archived/migration-assistants/php-to-aspnet/

Number 9
  • 615
  • 3
  • 9
1

You can automatically translate a small subset of PHP into C# using the transpiler library for SWI-Prolog. This is a short program that uses this library:

:- use_module(library(transpiler)).
:- set_prolog_flag(double_quotes,chars).
:- initialization(main).

main :- 
    Input = "function add($a,$b){return $a.$b;}function squared($a){return $a*$a;}function add_exclamation_point($parameter){return $parameter.\"!\";}",
    translate(Input,'php','c#',X),
    atom_chars(Y,X),
    writeln(Y).

This is the program's output in C#:

public static string add(string a,string b){
        return a+b;
}
public static int squared(int a){
        return a*a;
}
public static string add_exclamation_point(string parameter){
        return parameter+"!";
}
Anderson Green
  • 30,230
  • 67
  • 195
  • 328
-3
$sql = "SELECT num.name,num.on from table WHERE on = '0'";

$query = mysql_query($sql);

$arr =mysql_fetch_array($query);

$s = array($arr['name']);

if(in_array('1',$s)){echo "yes";}else{echo "no";} 
Jobins John
  • 1,265
  • 23
  • 45