Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
这个C#编码代码的PHP等价物是什么?_C#_Php - Fatal编程技术网

这个C#编码代码的PHP等价物是什么?

这个C#编码代码的PHP等价物是什么?,c#,php,C#,Php,我想把下面的C#代码转换成PHP C#是: 我把它变成了这样: $hash = hash_hmac( "sha256", utf8_encode("getfaqs"), utf8_encode("Password")); 那么我有: var apiKey = "ABC-DEF1234"; var authInfo = apiKey + ":" + hash //base64 encode the authorisation info var authorisationHeader = Con

我想把下面的C#代码转换成PHP

C#是:

我把它变成了这样:

$hash = hash_hmac( "sha256", utf8_encode("getfaqs"), utf8_encode("Password"));
那么我有:

var apiKey = "ABC-DEF1234";
var authInfo = apiKey + ":" + hash

//base64 encode the authorisation info
var authorisationHeader = Convert.ToBase64String(Encoding.UTF8.GetBytes(authInfo));
我认为应该是:

$authInfo = base64_encode($apiKey . ":" . $hash);

但不确定,请注意第二种编码使用encoding.UTF8,而不是UTF8Encoding.UTF8

PHP代码应该是什么样的?

PHP字符串已经(有点)
字节[]
,PHP没有任何编码意识
utf8_encode
实际上将ISO-8859-1转换为UTF-8,所以这里不需要它

如果这些字符串是文件中的文本,则该文件只需以UTF-8编码保存

true
传递给
hash\u hmac
作为第四个参数,并删除那些
utf8\u encode
调用:

$hash = hash_hmac( "sha256", "getfaqs", "Password", true );
此外,字符串连接运算符是
,因此:

$authInfo = base64_encode($apiKey . ":" . $hash);

UTF8编码.UTF8
Encoding.UTF8
是等效的
UTF8Encoding
继承自
Encoding
,因此它获得了
UTF8
属性。感谢您捕获错误更正。仍然没有进行身份验证,但我认为您的代码很好&我们有额外的problems@jmadsen是的,
C#
不完整。在此行之前:
var apiKey=“ABC-DEF1234”;var authInfo=apiKey+“:“+hash
hash
仍然是一个
字节[]
,因此如何将其连接到字符串?
$hash = hash_hmac( "sha256", "getfaqs", "Password", true );
$authInfo = base64_encode($apiKey . ":" . $hash);