Thread: Soap
View Single Post
Old Jan 3rd, 2008, 4:35 PM   #7
grimpirate
King of Portal
 
grimpirate's Avatar
 
Join Date: Sep 2005
Posts: 431
Rep Power: 4 grimpirate is on a distinguished road
Send a message via Yahoo to grimpirate
Re: Soap

Well this is what I ended up doing:
client.php (running wherever)
<?php
echo '<pre>';
print_r(parsePILgrim('http://redpando.110mb.com/remote.php'));
print_r(parsePILgrim('http://redpando.110mb.com/remote.php?procedure=geoLocate'));
print_r(parsePILgrim('http://redpando.110mb.com/remote.php?procedure=geoLoce'));
print_r(parsePILgrim('http://redpando.110mb.com/remote.php?procedure=geoLocate&param_0=' . $_SERVER['REMOTE_ADDR']));
echo '</pre>';

function parsePILgrim($url){
	if(false !== $request = get_headers($url, 1)){
		$statusCode = intval(substr(reset($request), strpos(reset($request), ' ') + 1, 3));
		if(isset($request['PILgrim'])){
			$request = unserialize($request['PILgrim']);
			foreach($request as $keys => $values){
				if(isset($values)){
					$request[$keys] = rawurldecode($values);
				}
			}
			return $request;
		}else{
			return !trigger_error($statusCode, E_USER_NOTICE);
		}
	}else{
		return !trigger_error('Request failed.', E_USER_NOTICE);
	}
}
?>
And then:
remote.php (running elsewhere)
<?php
if(isset($_GET['procedure'])){
	if(!strcmp($_GET['procedure'], 'geoLocate')){
		if(isset($_GET['param_0'])){
			if(ip2long($_GET['param_0']) === false){
				header("HTTP/1.1 405 Method Not Allowed");
			}else{
				$geoData = geoLocate('http://api.hostip.info/?' . $_GET['param_0']);
				if($geoData === false){
					header("HTTP/1.1 503 Service Unavailable");
				}else{
					header("PILgrim: " . serialize($geoData));
				}
			}
		}else{
			header("HTTP/1.1 405 Method Not Allowed");
		}
	}else{
		header("HTTP/1.1 400 Bad Request");
	}
}else{
	header("HTTP/1.1 400 Bad Request");
}

function geoLocate($url){
	// Standard way
	//$xmlString = @file_get_contents($url);

	// CURL way
	$ch = curl_init();
	$timeout = 0; // set to zero for no timeout
	curl_setopt ($ch, CURLOPT_URL, $url);
	curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
	$xmlString = @curl_exec($ch);
	$mimeType = strtolower(curl_getinfo($ch, CURLINFO_CONTENT_TYPE));
	$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
	$curlErr = curl_error($ch);
	curl_close($ch);

	if($httpCode !== 200){
		return $httpCode;
	}elseif($xmlString === false || strpos($mimeType, 'xml') === false || $curlErr != ''){
	//if($xmlString === false || strpos($xmlString, '<?xml') !== 0){
		return false;
	}else{
		$parser = xml_parser_create();
		xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
		xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
		xml_parse_into_struct($parser, $xmlString, $vals);
		xml_parser_free($parser);
		$xmlString = array();
		$parser = array();
		for($i = 0; $i < count($vals) - 1; $i++){
			if(isset($vals[$i]['value'])){
				$xmlString[$vals[$i]['tag']] = $vals[$i]['value'];
			}
		}
		$parser['location'] = rawurlencode(strval($xmlString['gml:name']));
		$parser['country'] = rawurlencode(strval($xmlString['countryName']));
		if(isset($xmlString['gml:coordinates'])){
			$parser['longitude'] = rawurlencode(floatval(array_shift(explode(',', $xmlString['gml:coordinates']))));
			$parser['latitude'] = rawurlencode(floatval(array_pop(explode(',', $xmlString['gml:coordinates']))));
		}else{
			$parser['longitude'] = null;
			$parser['latitude'] = null;
		}
		return $parser;
	}
}
?>
So now basically at the page http://redpando.110mb.com/remote.php I have a remote procedure that can be invoked from another page and return data to that page. It functions by making a GET request via the URL so if you wanted to use the geoLocate function in order to determine the geoLocation of an ip you would input something like this http://redpando.110mb.com/remote.php?procedure=geoLocate&param_0=127.0.0.1 The bolded area is what controls the function you're calling and the parameters being passed to it. It will generate a header line of PILgrim: some PHP serialized value in response to the request.
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis
grimpirate is offline   Reply With Quote