我需要使用私有X509密钥签署xml数据类型 这是我用过的代码: $ DATA->保存( “的test.xml”);
$data = new DOMDocument("1.0", "utf-8");
$xml_order = $data->createElement("order");
$xml_tag1 = $data->createElement("signature","000");
$xml_tag2 = $data->createElement("service","000");
$xml_tag3 = $data->createElement("url");
$xml_tag_confirm = $data->createElement("confirm","yes");
$attr = $data->appendChild( $xml_order);
$attr->appendChild($xml_tag1);
$attr->appendChild($xml_tag2);
$url = $attr->appendChild($xml_tag3);
$url->appendChild($xml_tag_confirm);
$attr->setAttribute("type","ama");
$attr->setAttribute("id", "passwordiguess");
$attr->setAttribute("date", date("YmdHis"));
$signature;
$pkeyid = openssl_pkey_get_private(file_get_contents('./private.key'));
$keyData = openssl_pkey_get_details($pkeyid);
openssl_sign($data, $signature, $pkeyid);
openssl_free_key($pkeyid);
它返回的错误是:
警告:openssl_sign()期望参数1为字符串,第32行的C:\ xampp \ htdocs \ test \ index.php中给出的对象
答案 0 :(得分:1)
问题是您将文件作为参数传递,而不是openssl_sign
函数中的字符串
您应该将xml文档转换为字符串,尝试添加此行
$data = new DOMDocument("1.0", "utf-8");
$xml_order = $data->createElement("order");
$xml_tag1 = $data->createElement("signature","000");
$xml_tag2 = $data->createElement("service","000");
$xml_tag3 = $data->createElement("url");
$xml_tag_confirm = $data->createElement("confirm","yes");
$attr = $data->appendChild( $xml_order);
$attr->appendChild($xml_tag1);
$attr->appendChild($xml_tag2);
$url = $attr->appendChild($xml_tag3);
$url->appendChild($xml_tag_confirm);
$attr->setAttribute("type","ama");
$attr->setAttribute("id", "passwordiguess");
$attr->setAttribute("date", date("YmdHis"));
/* this would convert the xml document to string */
$output = $data->saveXML();
$signature;
$pkeyid = openssl_pkey_get_private(file_get_contents('./private.key'));
$keyData = openssl_pkey_get_details($pkeyid);
/* then pass the $output to openssl_sign*/
openssl_sign($output, $signature, $pkeyid);
openssl_free_key($pkeyid);