Asking for improvement: TEL query for Asterisk on OC 9.1

Hi all,
Somebody knows how this script could be made faster? I got a delay of 2-3 secs for it to load.

<?php

    // This script does a query on OC 9.1 contacts for a given phonenumber
// Useful for Asterisk or so.

// Script located at the OC Docroot
// Usage: HTTP/s Request: script.php?login=<login>&pass=<pass>&asteriskquery&tel=<phonennumber>
// error_reporting(E_ALL & ~(E_STRICT|E_NOTICE));

// Issue: The whole loading is slow.
// Issue: getContactsManager()->search() will overwrite double TEL entries.
// this happens in vCard2Array() in:
// /apps/dav/lib/CardDAV/AddressBookImpl.php
if (isset($_GET['login']) && isset($_GET['pass'])){

    $_SERVER['PHP_AUTH_USER'] = $_GET['login'];
$_SERVER['PHP_AUTH_PW'] = $_GET['pass'];
} elseif(isset($_POST['login']) && isset($_POST['pass'])){

    $_SERVER['PHP_AUTH_USER'] = $_POST['login'];
$_SERVER['PHP_AUTH_PW'] = $_POST['pass'];
} else {
	
	die();
	
}

if(isset($_GET['asteriskquery'])){

    $querynum = $_GET['tel'];


require_once 'lib/base.php';

// Patched together solely from base.php
$request = \OC::$server->getRequest();
$userSession = \OC::$server->getUserSession();
$userSession->tryBasicAuthLogin($request);


$cm = \OC::$server->getContactsManager();

$result = $cm->search("", array("TEL"));

$collect= array();
foreach ($result as $id => $arr){
	foreach($arr as $key => $value) {
	    if (preg_match("/^TEL/",$key)){
			// needed or not
			$thistel = str_replace(" ","",$value);
			$thistel = str_replace(" ","",$thistel);
			$thistel = str_replace("+41","0",$thistel);
			$thistel = preg_replace("/^0041/", "0", $thistel);
			$collect[$thistel]=$arr['FN'];
	    }
	}						
}
// echo the FN for this TEL
echo $collect[$querynum];
}

btw, for the brave: this is my vCard2Array().
I dont know if it brakes anything in OC, but since it is destructive on its own (TEL overwritten in the original), I guess it is harmless:

	# MANU EDIT
	protected function vCard2Array($uri, VCard $vCard) {
		$result = [
			'URI' => $uri,
		];

    	foreach ($vCard->children as $property) {
		if ($property->name === 'PHOTO' && $property->getValueType() === 'BINARY') {
			$url = $this->urlGenerator->getAbsoluteURL(
				$this->urlGenerator->linkTo('', 'remote.php') . '/dav/');
			$url .= implode('/', [
				'addressbooks',
				substr($this->addressBookInfo['principaluri'], 11), //cut off 'principals/'
				$this->addressBookInfo['uri'],
				$uri
			]) . '?photo';

			$result['PHOTO'] = 'VALUE=uri:' . $url;
		} else {
			$thiskey = $property->name;
			$thisvalue = $property->getValue();
			$i=1;
			# Produces TEL-$i numbered up
			if(isset($result[$thiskey]) && ($result[$thiskey] != $thisvalue)){ 
				while(isset($result[$thiskey."-".$i])){
					$i++;
				}
				$thiskey = $thiskey."-".$i;
			}
			$result[$thiskey] = $thisvalue;
		}
	}
	if ($this->addressBookInfo['principaluri'] === 'principals/system/system' &&
		$this->addressBookInfo['uri'] === 'system') {
		$result['isLocalSystemBook'] = true;
	}
 		
		return $result;
	}