php到C#转换器
我需要将此PHP代码转换为C#。有没有一个工具或网站可以让这成为可能php到C#转换器,c#,php,C#,Php,我需要将此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'; } $
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;
}
公共函数调用($method,array$params){
//添加format参数,目前只支持“json”
如果(!array_key_存在('format',$params)){
$params['format']='json';
}
$url=“{$this->url}/{$method}”;
$ch=$this->\u getCurlHandle($url);
如果(!$ch){
抛出新的Fuze_客户端_异常(“无法创建卷曲句柄”);
}
//设置请求参数
$queryString=http\u build\u query($params);
curl_setopt($ch,CURLOPT_POSTFIELDS,$queryString);
//开火!
$result=$this->\u executeCurl($ch);
//所有API响应有效负载都应该是带有“code”和
//“消息”成员
$json=json_decode($result);
if(!($json instanceof stdClass)
||!isset($json->code)
||!isset($json->message)){
抛出新Fuze\u客户端\u服务器异常(
“接收到无效的JSON有效负载”、$result、$info['http_code']);
}
如果($json->code>=500&&$json->code<600){
抛出新的Fuze\u客户端\u FaultException($json);
}
返回$json;
}
我不知道有什么工具可以从PHP转换成C。如果有这样的事情,我不会相信它能正确地进行转换。因此,您可以选择:
- 学习C#
- 把它传给能转换它的人
- 这可能会有所帮助。
它有一个转换引擎,但事实上它得到了“白皮书”的支持
这让我相信,前面还有很艰难的路要走
也许你应该看看。它不是一个转换器,但它允许您为.Net编译PHP代码——因此您应该能够从C#调用PHP方法。
这是一个开源项目,源代码是托管的 没有转换器,但您可以看看。它是一种多平台的开源语言,可以编译成其他语言,包括C#。语法类似于PHP
无论如何,对于这个代码段,手动转换是最好的方法。您可以使用SWI Prolog的
transpiler
库将一小部分PHP自动转换为C。这是一个使用此库的简短程序:
$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";}
:- 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#输出:
使用我们正在开发的PHP编译器,使PHP项目成为常规的.NET项目(如F#或C++/CLI),而不是转换(这将导致无法管理的意大利面代码):
通过这种方式,除其他事项外,您可以保持PHP代码的可维护性,同时可以从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+"!";
}