示例代码:
function ChinaPayUrl($html){
// 创建一个新的DOMDocument实例
$dom = new \DOMDocument();
// 加载HTML内容
@$dom->loadHTML($html);
// 创建一个DOMXPath实例
$xpath = new \DOMXPath($dom);
// 获取action
$formElements = $xpath->query("//form"); // 使用XPath查询语法获取所有form元素
$action = $formElements[0]->getAttribute('action');
// 获取params
$inputElements = $xpath->query("//input"); //使用XPath查询语法获取所有input元素
$params = [];
foreach ($inputElements as $input) {
$params[$input->getAttribute('name')] = $input->getAttribute('value');
}
// 拼接url
return $action . '?' . http_build_query($params);
}
$a = "<!DOCTYPE html>
<html lang=\"en\">
<head>
<meta charset=\"UTF-8\">
<title>Document</title>
</head>
<body>
<form action='http://test.lumen.cn/a.php'>
<input type='text' name='username' value='admin'>
<input type='password' name='pwd' value='111111'>
<input type='submit' value='submit'>
</form>
</body>
</html>";
$res = ChinaPayUrl($a);
var_dump($res);
结果为:string(60) "http://test.lumen.cn/a.php?username=admin&pwd=111111&=submit"
标签:xpath,form,URL,getAttribute,params,action,input,PHP From: https://www.cnblogs.com/songcheng-nick/p/18254977