<?php /**/ ?><?php

	$api_key = "e81ef8102a5160154ef4662adcc9046b";

// generic method for retrieving content for a given url.
	function getResource($url){
		$chandle = curl_init();
		curl_setopt($chandle, CURLOPT_URL, $url);
		curl_setopt($chandle, CURLOPT_RETURNTRANSFER, 1);
		$result = curl_exec($chandle);
		curl_close($chandle);

		return $result;
	}

	# return simplexml object for $url if successful with specified number of retries
	function flickrCall($url,$retries) {
		$success = false;
		for ($retry = 0; $retry < $retries; $retry++) {
			$rsp = getResource($url);
			$xml = simplexml_load_string($rsp);
			if ($xml["stat"] == 'ok') {
				$success = true;
				break;
			}
		} // for
		if ($success) {
			return $xml;
		} else {
			throw new Exception("Could not successfully call Flickr");
		}
	}

# go through all the methods and list

	function getMethods($api_key) {

	// e.g., http://api.flickr.com/services/rest/?method=flickr.reflection.getMethods&api_key=e81ef8102a5160154ef4662adcc9046b
	// would be useful to return this as an array  (later on, I can have another method to group them under common prefixes.)

		$url = "http://api.flickr.com/services/rest/?method=flickr.reflection.getMethods&api_key={$api_key}";
		$xml = flickrCall($url, 3);
		foreach ($xml->methods->method as $method) {
			//print "${method}\n";
			$method_list[] = (string) $method;
		}
		return $method_list;
	}

# get info about a given method($api_key, $method_name)

	function getMethodInfo($api_key,$method_name) {

		$url =
		"http://api.flickr.com/services/rest/?method=flickr.reflection.getMethodInfo&api_key={$api_key}&method_name={$method_name}";
		$xml = flickrCall($url, 3);
		return $xml;
	}

# store the data
	function store_api_data($fname, $methods, $methods_info) {

		$to_store['methods'] = $methods;
		$to_store['methods_info'] = $methods_info;
		$to_store_str = serialize($to_store);
		$fh = fopen($fname,'wb') OR die ("can't open $fname!");
		$numbytes = fwrite($fh, $to_store_str);
		fclose($fh);
	}

# restore the data

	function restore_api_data($fname) {

		$fh = fopen($fname,'rb') OR die ("can't open $fname!");
		$contents = fread($fh, filesize($fname));
		fclose($fh);
		return unserialize($contents);

	}

#-----------------------------------------------------------

	function download_Flickr_API ($api_key, $fname) {

	$methods = getMethods($api_key);

	// now loop to grab info for each method

	$limit = 1000;
	$count = 0;

	foreach ($methods as $method) {

		$count += 1;
		if ($count > $limit) {
			break;
		}

	 //print "${method}\n";
		$xml = getMethodInfo($api_key,$method);
	//   print_r($xml);
		$method_array["needslogin"] = (integer) $xml->method["needslogin"];
		$method_array["needssigning"] = (integer) $xml->method["needssigning"];
		$method_array["requiredperms"] = (integer) $xml->method["requiredperms"];
		$method_array["description"] = (string) $xml->method->description;
		$method_array["response"] = (string) $xml->method->response;
	// loop through the arguments
		$args = array();
		foreach ($xml->arguments->argument as $argument) {
			$arg["name"] = (string) $argument["name"];
			$arg["optional"] = (integer) $argument["optional"];
			$arg["text"] = (string) $argument;
			$args[] = $arg;
		}
		$method_array["arguments"] = $args;

	// loop through errors
		$errors = array();
		foreach ($xml->errors->error as $error) {
			$err["code"] = (string) $error["code"];
			$err["message"] = (integer) $error["message"];
			$err["text"] = (string) $error;
			$errors[] = $err;
		}
		$method_array["errors"] = $errors;


		$methods_info[$method] = $method_array;
	}

	//  var_dump($methods_info);

		store_api_data($fname, $methods, $methods_info);
} // download_Flickr_API


#-----------------------------------------------------------

		$fname = 'flickr.methods.info.txt';;
//    download_Flickr_API($api_key, $fname);

		$m = restore_api_data($fname);
		$methods = $m["methods"];
		$methods_info = $m["methods_info"];
//    print_r($methods_info);

?>
<html>
	<head>
	</head>
	<body>
		<table>
			<tr>
				<th>method name</th>
				<th>description</th>
				<th>needs login</th>
				<th>needs signing</th>
				<th>permissions</th>
				<th>args (mandatory)</th>
				<th>args (optional)</th>
			</tr>
<?php
	foreach ($methods_info as $name=>$method) {
		$description = $method["description"];
# calc mandatory and optional arguments
		$m_args = "";
		$o_args = "";
		foreach ($method["arguments"] as $arg){
			//print "arg: {$arg['name']}\n";
			//print_r ($arg);
			// don't list api_key since it is mandatory for all calls
			if ($arg['name'] != 'api_key') {
				if ($arg["optional"] == '1') {
					$o_args .= " {$arg['name']}";
				} else {
					$m_args .= " {$arg['name']}";
				}
			} //if
		}
		print <<<EOT
			<tr>
				<td><a href="http://www.flickr.com/services/api/{$name}.html">{$name}</a></td>
				<td>{$description}</td>
				<td>{$method["needslogin"]}</td>
				<td>{$method["needssigning"]}</td>
				<td>{$method["requiredperms"]}</td>
				<td>{$m_args}</td>
				<td>{$o_args}</td>
			</tr>
EOT;
	}
?>
		</table>
	</body>
</html>
