2011-03-07 65 views
10

我需要将此PHP代码转换为C#。有没有一种工具或网站可以使这成为可能?php转换为C#转换器

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; 
} 
+0

这似乎是一个更大的东西的一部分。你需要翻译什么?卷曲部分? – 2011-03-07 15:45:00

+0

相关:http:// stackoverflow。com/questions/441161/how-to-convert-code-from-c-to-php – RQDQ 2011-03-07 15:45:30

+0

可能不是你要找的,但Facebook的HipHop for PHP将PHP转换为C++。 (是的,我知道你在问C#,但这是我能想到的最接近的东西:D)。如果这就是你所追求的目标,它并不能真正帮你转换代码,但会提高你的性能。 – Sondre 2011-03-07 15:55:33

回答

31

我不知道任何可以从PHP转换到C#的工具。如果有这样的事情,我不会相信它正确地进行转换。所以离开你的选项:

  • 学习C#
  • 它传给别人谁可以把它转换。
+2

+1,借调。将源代码从一种语言转换为另一种语言是一项复杂的任务,我怀疑是否有一种工具可以使其成为可能,更不用说可靠性了。 – mingos 2011-03-07 15:50:59

+3

除了转换之外,还可以将现有的PHP代码集成到.NET中,例如。使用Phalanger提到在http://stackoverflow.com/questions/5221669/php-to-c-sharp-converter/8007020#8007020它正在使用 – 2012-03-28 11:53:45

+0

@JakubMíšek,然后逆向工程,哈哈。实际上,转换简单块不会太难,只是没有人认为值得去解决 – 2015-09-11 12:19:13

16

也许你应该看一看Phalanger。它不是一个转换器,但它可以让你为.Net编译PHP代码 - 所以你应该可以从C#调用php方法。 这是一个开源项目,源代码为here

3

没有转换器,但你可以看看Haxe。它是多平台的开源语言,可以编译成其他语言,包括C#。该语法与PHP类似。

无论如何,对于此代码段手动转换将是最好的办法。

-1
$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";} 
0

您可以使用SWI-Prolog的transpiler库自动将PHP的一小部分转换为C#。这是一个使用这个库中的短节目:

:- 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). 

这档节目在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+"!"; 
}