PHP docking with Ethereum
Publish: 2021-04-21 02:29:56
1. curl方法,file_get_contents,
2. Through curl to simulate, and then get the data, and use it in JSON format
3.
Ethereum specifies the JSON RPC API application development interface that each node needs to implement. This interface is transport independent. Applications can use this interface protocol to operate Ethereum nodes through HTTP, websocket or IPC and other communication mechanisms:

4. t you," the matron said, "
5. Fixed output or send request format, this is an interface to understand what the payment interface is
6. I don't know about it, but I've seen that the file from the attendance machine is TXT. You can use it to read the data in it. It should be similar to ordinary reading TXT
7.
Personal suggestion:

8. Pro, it's not like this. After the program made by PHP runs, it will output HTML on the browser, and then the data scanned by the scanner will be written into an input box, and then submit the form. After receiving the request, PHP will operate the database
because the scanner gun is a standard input device, there is no need for additional matching
such as
& lt; input type=" text" name=" code" /& gt;, When the element gets the focus, you can enter the content by pressing the keyboard or by using the scanner gun. Understand
useful, hope to adopt it
because the scanner gun is a standard input device, there is no need for additional matching
such as
& lt; input type=" text" name=" code" /& gt;, When the element gets the focus, you can enter the content by pressing the keyboard or by using the scanner gun. Understand
useful, hope to adopt it
9. The key generated by Java is transformed into the key in PEM format which can be recognized by PHP, and the other signature adding and verification are performed by calling OpenSSL built-in signature method
the key generated by Java is mainly string; The PEM format key is a 64 bit line with file formats such as the beginning and end. Then PHP gets the corresponding PEM format string
PEM format key
--- begin public key -----
/ / 64chars one line (multiple lines)
--- End public key -----
--- begin RSA private key -----
/ / 64chars one line (multiple lines)
--- End RSA private key -----
1. PHP code for Java key to PEM format
/ * *
* format string format public and private key to PEM format public and private key
* @ param $se cret_ key
* @param $type
* @return string
*/
public static function format_ secret_ key($secret_ Key, $type) {
/ / 64 English characters followed by the newline character & quot 92; n", Finally, it is followed by the newline character & quot 92; n"< br />$key = (wordwrap($secret_ key, 64, " 92; n", true))." 92; n";
/ / add the header and tail of PEM format
if ($type = = & 39; pub') {< br />$pem_ key = "----- BEGIN PUBLIC KEY-----\ n" . $ key . "----- END PUBLIC KEY-----\ n";< br />}else if ($type == ' pri') {< br />$pem_ key = "----- BEGIN RSA PRIVATE KEY-----\ n" . $ key . "----- END RSA PRIVATE KEY-----\ n";< br />}else{
echo(' Private key type is illegal< br />exit();< br />}
return $pem_ key;
}
2. Signature
/ *
* RSA signature
* @ param $paramstr
* @ param $prikey
* @ return string
* /
public static function sign ($paramstr, $prikey) {
$sign = & # 39 39;;
/ / convert the string format public-private key to PEM format public-private key
$prikeypem = signutil:: format_ secret_ key($priKey, ' pri');
/ / converted to OpenSSL key, it must be a private key without pkcs8 conversion
$res = OpenSSL_ get_ privatekey($priKeyPem);
/ / call the OpenSSL built-in signature method to generate the signature $sign
OpenSSL_ sign($paramStr, $sign, $res);
/ / release resources
OpenSSL_ free_ key($res);
/ / Base64 encoded signature
$signbase64 = Base64_ encode($sign);
/ / url encoded signature
$sign = URLEncode ($signbase64)< br />return $sign;
}
3. Signature verification
/ *
* RSA signature verification
* @ param $paramstr
* @ param $sign
* @ param $pubkey
* @ return bool
* /
public static function verify ($paramstr, $sign, $pubkey) {
/ / change the string format public-private key to PEM format public-private key
$pubkeypem = signutil:: format_ secret_ key($pubKey, ' pub');
/ / converted to OpenSSL key, it must be a public key without pkcs8 conversion
$res = OpenSSL_ get_ publickey($pubKeyPem);
/ / url decode signature
$signurl = URLDecode ($sign))
/ / Base64 decode signature
$signbase64 = Base64_ decode($signUrl);
/ / call the OpenSSL built-in method to verify the signature, and return the bool value
$result = (bool) OpenSSL_ verify($paramStr, $signBase64, $res);
/ / release resources
OpenSSL_ free_ key($res);
/ / returns whether the resource is successful
return $result< br />}
the key generated by Java is mainly string; The PEM format key is a 64 bit line with file formats such as the beginning and end. Then PHP gets the corresponding PEM format string
PEM format key
--- begin public key -----
/ / 64chars one line (multiple lines)
--- End public key -----
--- begin RSA private key -----
/ / 64chars one line (multiple lines)
--- End RSA private key -----
1. PHP code for Java key to PEM format
/ * *
* format string format public and private key to PEM format public and private key
* @ param $se cret_ key
* @param $type
* @return string
*/
public static function format_ secret_ key($secret_ Key, $type) {
/ / 64 English characters followed by the newline character & quot 92; n", Finally, it is followed by the newline character & quot 92; n"< br />$key = (wordwrap($secret_ key, 64, " 92; n", true))." 92; n";
/ / add the header and tail of PEM format
if ($type = = & 39; pub') {< br />$pem_ key = "----- BEGIN PUBLIC KEY-----\ n" . $ key . "----- END PUBLIC KEY-----\ n";< br />}else if ($type == ' pri') {< br />$pem_ key = "----- BEGIN RSA PRIVATE KEY-----\ n" . $ key . "----- END RSA PRIVATE KEY-----\ n";< br />}else{
echo(' Private key type is illegal< br />exit();< br />}
return $pem_ key;
}
2. Signature
/ *
* RSA signature
* @ param $paramstr
* @ param $prikey
* @ return string
* /
public static function sign ($paramstr, $prikey) {
$sign = & # 39 39;;
/ / convert the string format public-private key to PEM format public-private key
$prikeypem = signutil:: format_ secret_ key($priKey, ' pri');
/ / converted to OpenSSL key, it must be a private key without pkcs8 conversion
$res = OpenSSL_ get_ privatekey($priKeyPem);
/ / call the OpenSSL built-in signature method to generate the signature $sign
OpenSSL_ sign($paramStr, $sign, $res);
/ / release resources
OpenSSL_ free_ key($res);
/ / Base64 encoded signature
$signbase64 = Base64_ encode($sign);
/ / url encoded signature
$sign = URLEncode ($signbase64)< br />return $sign;
}
3. Signature verification
/ *
* RSA signature verification
* @ param $paramstr
* @ param $sign
* @ param $pubkey
* @ return bool
* /
public static function verify ($paramstr, $sign, $pubkey) {
/ / change the string format public-private key to PEM format public-private key
$pubkeypem = signutil:: format_ secret_ key($pubKey, ' pub');
/ / converted to OpenSSL key, it must be a public key without pkcs8 conversion
$res = OpenSSL_ get_ publickey($pubKeyPem);
/ / url decode signature
$signurl = URLDecode ($sign))
/ / Base64 decode signature
$signbase64 = Base64_ decode($signUrl);
/ / call the OpenSSL built-in method to verify the signature, and return the bool value
$result = (bool) OpenSSL_ verify($paramStr, $signBase64, $res);
/ / release resources
OpenSSL_ free_ key($res);
/ / returns whether the resource is successful
return $result< br />}
Hot content
