从 url 获取页面上的元素的值并使用它


Getting a a value from a Element on a page from a url and using it

我想做的是获取带有标签"input"和名称"__RequestVerificationToken"的元素的值并使用它,而无需刷新页面...如果这有任何意义。

我知道 curl,我现在正在做的是从页面获取内容,然后使用 DOM 获取 __RequestVarificationToken 的值并在 curl POST 中使用它。这是我目前的代码,代码不起作用...我怎样才能让它工作?

<?php 
    function curl($url, $post=false, $cookie=false){
    $ch = curl_init();
    curl_setopt ($ch, CURLOPT_URL, $url );
    curl_setopt ($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_REFERER, $url);
    curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
    curl_setopt($ch, CURLOPT_AUTOREFERER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 60);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
   
    $agent = "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)";
    curl_setopt($ch, CURLOPT_USERAGENT, $agent);
    if($cookie){
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
    }
   
    if($post){
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    }
   
    return  curl_exec ($ch);
}
function SendMsg($body,$Subject,$RecipientId){
	$dom = new DOMDocument;
	$Content = curl("http://m.roblox.com/messages/sendmessage?id=$RecipientId", false, USERNAME);
	//print_r($Content);
	$dom->loadHTML($Content);
	$input = $dom->getElementsByTagName('input');
	foreach($input as $node){
		print_r($node);
		if ($node->nodeName == '__RequestVerificationToken'){
			   $vartoken = $node->nodeValue;
		}
	}
	curl("http://m.roblox.com/messages/sendmessagework",('__RequestVerificationToken=' .$vartoken . '&RecipientId='.$RecipientId. '&Subject='.$Subject.'&Body='.$body),USERNAME);
	echo("Message sent?");
} ?>
任何帮助将不胜感激!谢谢。

如果您指的是名称属性值

<input name="name_here" /> <!-- name attribute -->

那么$node->nodeName就不是你要找的。

请改用->getAttribute()

if($node->getAttribute('name') == '__RequestVerificationToken'){
    $vartoken = $node->nodeValue;
}