Difference between revisions of "User:Jor3l Boa/PHP/n2k.php"

From Second Life Wiki
Jump to navigation Jump to search
m (language tags to <source>)
 
(13 intermediate revisions by 4 users not shown)
Line 2: Line 2:


n2k.php:
n2k.php:
<php>
//  PHP name2Key by Jor3l Boa
//  -------------------------
//  Released under Creative Commons 3.0
//  - support+n2k@sltools.biz
//  -------------------------


         function getPage($web) {
<source lang="php">
<?php
/*
    PHP name2Key by Jor3l Boa
    -------------------------
    Released under Creative Commons 3.0
    - devi@foravatars.com
    -------------------------
    Updates (DD/MM/YYYY)
    25/09/2010 - Jor3l Boa: Removed some bites // Om nom nom nom
    12/08/2010 - Qwerty Venom: Updated version of n2k.php that will work with the new design
*/
         function getPage($web)
{
$html = "";
$html = "";
  $ch = curl_init($web);
  $ch = curl_init($web);
Line 19: Line 27:
  curl_setopt($ch, CURLOPT_TIMEOUT, 5);
  curl_setopt($ch, CURLOPT_TIMEOUT, 5);
  $html = curl_exec($ch);
  $html = curl_exec($ch);
  if(curl_errno($ch)) {
  if(curl_errno($ch))
  {
  $html = "";
  $html = "";
  }
  }
Line 25: Line 34:
return $html;
return $html;
}
}
         function getBetween($content,$start,$end){
 
         function getBetween($content,$start,$end)
{
//by Jor3l :)
//by Jor3l :)
$a1 = strrpos($content,$start);
$a1 = strrpos($content,$start);
$content = substr($content,$a1 + (strlen($start)),strlen($content));
$content = substr($content,$a1 + strlen($start));
while($a2 = strrpos($content,$end)) {
while($a2 = strrpos($content,$end))
{
$content = substr($content,0,$a2);
$content = substr($content,0,$a2);
}
}
return $content;
return $content;
}
}
function name2Key($name) {
function name2Key($name)
$SL_SEARCH = 'http://search.secondlife.com/client_search.php?session=00000000-0000-0000-0000-000000000000&q=';
{
$sName = split(' ',$name);
                $NULL = '00000000-0000-0000-0000-000000000000';
$data = getPage($SL_SEARCH.$sName[0].'%2520'.$sName[1]);
$SL_SEARCH = "http://search.secondlife.com/client_search.php?session=$NULL&q=";
$uuid = getBetween($data,'secondlife:///app/agent/','/about');
$sName = explode(' ',$name); //Qwerty changed split to explode
$data = getPage($SL_SEARCH.$sName[0].'+'.$sName[1]);
$uuid = getBetween($data,'http://world.secondlife.com/resident/','"');
                if(!preg_match("/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/",$uuid)) $uuid = $NULL;
return $uuid;
return $uuid;
}
}
 
?>
</php>
</source>


example:
example:
<php>
<source lang="php">
<?
<?
require_once('n2k.php');
require_once('n2k.php');
Line 52: Line 67:
echo $uuid;
echo $uuid;
?>
?>
</php>
</source>


<b>NOTE ON HOSTING PROVIDERS:</b>
<b>NOTE ON HOSTING PROVIDERS:</b>
Some hosting providers, notably GoDaddy, do not allow outbound traffic from their servers.  If you find that this script returns nothing, no matter what you try, that may be your issue.  The only fix is to switch providers to one that does allow outbound traffic.<br/><br/>
Some hosting providers, notably GoDaddy, do not allow outbound traffic from their servers.  If you find that this script returns nothing, no matter what you try, that may be your issue.  The only fix is to switch providers to one that does allow outbound traffic.<br/><br/>
<I>For a version that does not require hosting, see also: [[Name2Key_in_LSL]]</i>
<I>For an LSL version, see also: [[Name2Key_in_LSL]]</i>

Latest revision as of 19:39, 24 January 2015

PHP Based name2Key service.

n2k.php:

<?php
/*
    PHP name2Key by Jor3l Boa
    -------------------------
    Released under Creative Commons 3.0
    - devi@foravatars.com
    -------------------------
    Updates (DD/MM/YYYY)
    25/09/2010 - Jor3l Boa: Removed some bites // Om nom nom nom
    12/08/2010 - Qwerty Venom: Updated version of n2k.php that will work with the new design
*/
 
        function getPage($web)
	{
		$html = "";
		  $ch = curl_init($web);
		  curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.12) Gecko/20070508 Firefox/1.5.0.12");
		  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		  curl_setopt($ch, CURLOPT_HEADER, 0);
		  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
		  curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
		  curl_setopt($ch, CURLOPT_TIMEOUT, 5);
		  $html = curl_exec($ch);
		  if(curl_errno($ch))
		  {
			  $html = "";
		  }
		  curl_close ($ch);
		return $html;
	}

        function getBetween($content,$start,$end)
	{
		//by Jor3l :)
		$a1 = strrpos($content,$start);
		$content = substr($content,$a1 + strlen($start));
		while($a2 = strrpos($content,$end))
		{
			$content = substr($content,0,$a2);
		}
		return $content;
	}
 
	function name2Key($name)
	{
                $NULL = '00000000-0000-0000-0000-000000000000';
		$SL_SEARCH = "http://search.secondlife.com/client_search.php?session=$NULL&q=";
		$sName = explode(' ',$name); //Qwerty changed split to explode
		$data = getPage($SL_SEARCH.$sName[0].'+'.$sName[1]);
		$uuid = getBetween($data,'http://world.secondlife.com/resident/','"');
                if(!preg_match("/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/",$uuid)) $uuid = $NULL;
		return $uuid;
	}
?>

example:

<?
require_once('n2k.php');
$uuid = name2key('FirstName LastName');
echo $uuid;
?>

NOTE ON HOSTING PROVIDERS: Some hosting providers, notably GoDaddy, do not allow outbound traffic from their servers. If you find that this script returns nothing, no matter what you try, that may be your issue. The only fix is to switch providers to one that does allow outbound traffic.

For an LSL version, see also: Name2Key_in_LSL