Difference between revisions of "User:Ina Centaur/PHP/k2n.php"

From Second Life Wiki
Jump to navigation Jump to search
(Created page with ' [code] function key2Name($q){ if(strlen($q)==36){ $target_url = "http://world.secondlife.com/resident/".$q; $userAgent = 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; r...')
 
m (language tags to <source>)
 
(5 intermediate revisions by one other user not shown)
Line 1: Line 1:
PHP Based key2Name service.


 
k2n.php:
[code]
<source lang="php">
//  PHP key2Name by Ina Centaur
//  -------------------------
//  Released under Creative Commons 3.0
//  - me at ina dot centaur dot com
//  -------------------------
function key2Name($q){
function key2Name($q){
if(strlen($q)==36){
if(strlen($q)==36){
Line 42: Line 48:
}
}
}
}
</source>
example:
<source lang="php">
<?
require_once('k2n.php');
$name = key2Name('INSERT 36-char UUID here');
echo $name;
?>
</source>


[/code]
Note: This is NOT a name2key service. [[https://wiki.secondlife.com/wiki/User:Jor3l_Boa/PHP/n2k.php You can find a name2Key here, by Jori]].

Latest revision as of 20:27, 24 January 2015

PHP Based key2Name service.

k2n.php:

//  PHP key2Name by Ina Centaur
//  -------------------------
//  Released under Creative Commons 3.0
//  - me at ina dot centaur dot com
//  -------------------------
function key2Name($q){
	if(strlen($q)==36){
		$target_url = "http://world.secondlife.com/resident/".$q;
		$userAgent = 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.0.16) Gecko/2009120208 Firefox/3.0.16 (.NET CLR 3.5.30729)';
		
		// curl it
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
		curl_setopt($ch, CURLOPT_URL,$target_url);
		curl_setopt($ch, CURLOPT_FAILONERROR, true);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
		curl_setopt($ch, CURLOPT_AUTOREFERER, true);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
		curl_setopt($ch, CURLOPT_TIMEOUT, 10);
		$html= curl_exec($ch);
		if (!$html) {
			echo "<br />cURL error number:" .curl_errno($ch);
			echo "<br />cURL error:" . curl_error($ch);
			exit;
		}
		
		// parse the html into a DOMDocument
		$dom = new DOMDocument();
		@$dom->loadHTML($html);
		
		// grab the name on the page (between title tags in head)
		$xpath = new DOMXPath($dom);
		$hrefs = $xpath->evaluate("/html/head/title");
		
		for ($i = 0; $i < $hrefs->length; $i++) {
			$href = $hrefs->item($i);
			$k2n = $href->nodeValue;
		}
		
		return $k2n;
	
	}else{
		return 'Please enter a 36-character UUID to find the name attached to that avatar key.';
	}	
}

example:

<?
require_once('k2n.php');
$name = key2Name('INSERT 36-char UUID here');
echo $name;
?>

Note: This is NOT a name2key service. [You can find a name2Key here, by Jori].