Commits

stollie  committed 48aac4c

json: matches en countries staat nu wel in een hoofd element, "matches" en "countries".
siteurl apart boven in pagina.
Docs.

  • Participants
  • Parent commits 56faf1f

Comments (0)

Files changed (4)

 RewriteRule ^countries.(xml|json)$ countries.php?type=$1
 
 RewriteRule ^matches/([0-9]{2}).(xml|json)$ matches.php?id=$1&type=$2
-RewriteRule ^matches.(xml|json)$ matches.php?type=$2
+RewriteRule ^matches.(xml|json)$ matches.php?type=$1

File countries.php

 //ini_set("display_errors",false) ;
 
 $method = $_SERVER['REQUEST_METHOD'];
+$siteurl = "http://localhost/ek-api/";
 
 if ( isset($_GET['shortcode']) && !empty($_GET['shortcode']) ) {
 	$shortcode = $_GET['shortcode'];
 			
 			if ( $type == 'json' ) {
 				// Add links
-				$array['links']['group'] = "http://localhost/ek-api/countries/".$array['group'].".".$type ;
+				$array['links']['group'] = $siteurl."countries/".$array['group'].".".$type ;
+				$array = array('countries' => $array);
 				echo json_encode($array);
 			}
 			else {			
 					$links = $doc->createElement( "links"  );
 					$country->appendChild( $links );
 					
-						$group = $doc->createElement( "group", "http://localhost/ek-api/countries/".$array['group'].".".$type  );
+						$group = $doc->createElement( "group", $siteurl."countries/".$array['group'].".".$type  );
 						$links->appendChild( $group );
 				
 				echo $doc->saveXML();
 			}
 			else {
 				$stmt = $db->query('SELECT * FROM countries');
-				
 			}
 		
 			$array = $stmt->fetchAll(PDO::FETCH_ASSOC);
 			if ( $type == 'json' ) {
 				$new_array = Array();
 				foreach($array as $row) {
-					$row['links']['self'] = "http://localhost/ek-api/countries/".$row['code'].".".$type ;
-					$row['links']['group'] = "http://localhost/ek-api/countries/".$row['group'].".".$type ;
+					$row['links']['self'] = $siteurl."countries/".$row['code'].".".$type ;
+					$row['links']['group'] = $siteurl."countries/".$row['group'].".".$type ;
 					$new_array[] = $row;
 				}
-
+				$new_array = array('countries' => $new_array);
 				echo json_encode($new_array);
 			}
 			else {
 					$links = $doc->createElement( "links"  );
 					$country->appendChild( $links );
 					
-						$self = $doc->createElement( "self", "http://localhost/ek-api/countries/".$row['code'].".".$type  );
+						$self = $doc->createElement( "self", $siteurl."countries/".$row['code'].".".$type  );
 						$links->appendChild( $self );
-						$group = $doc->createElement( "group", "http://localhost/ek-api/countries/".$row['group'].".".$type  );
+						$group = $doc->createElement( "group", $siteurl."countries/".$row['group'].".".$type  );
 						$links->appendChild( $group );
 				}
 				echo $doc->saveXML();
 			}
 		}	
 	 break;
+	 
 	/* Veranderen naar POST of aanpassen zodat het echt UPDATE wordt */
-	case 'PUT':		
-	
-		$inputSocket = fopen("php://input", "r");
-		$inputData = '';
-		
-		while ($data = fread($inputSocket, 1024) ) {
-			$inputData .= $data;
-		}
-		fclose($inputSocket);
-
-		$readData = new SimpleXMLElement($inputData);
-		
-		$code = (string)$readData->code;
-		$name = (string)$readData->name;
-		$group = (string)$readData->group;
+	case 'POST':		
+		$code = $_POST['code'];
+		$name = $_POST['name'];
+		$group = $_POST['group'];
 		
 		$stmt = $db->prepare("INSERT INTO `countries` (`code`, `name`, `group`) VALUES (:code, :name, :group)");
 		$stmt->bindParam(':code', $code , PDO::PARAM_STR, 2);
 			if ( $type == 'json' ) {
 				header( "content-type: application/json; charset=utf-8" );
 				
-				$array['links']['group'] = "http://localhost/ek-api/countries/".$array['group'].".".$type ;
+				$array['links']['group'] = $siteurl."countries/".$array['group'].".".$type ;
 				echo json_encode($array);
 			}
 			else {
 					$links = $doc->createElement( "links"  );
 					$country->appendChild( $links );
 					
-						$group = $doc->createElement( "group", "http://localhost/ek-api/countries/".$array['group'].".".$type  );
+						$group = $doc->createElement( "group", $siteurl."countries/".$array['group'].".".$type  );
+						$links->appendChild( $group );
+				
+				echo $doc->saveXML();
+			}	
+		}
+		else {
+			header('http/1.0 500 Internal Server Error');
+		}
+	 break;	 
+	 
+	/* Veranderen naar POST of aanpassen zodat het echt UPDATE wordt */
+	case 'PUT':		
+		$inputSocket = fopen("php://input", "r");
+		$inputData = '';
+		
+		while ($data = fread($inputSocket, 1024) ) {
+			$inputData .= $data;
+		}
+		fclose($inputSocket);
+
+		$readData = new SimpleXMLElement($inputData);
+		
+		$code = (string)$readData->code;
+		$name = (string)$readData->name;
+		$group = (string)$readData->group;
+		
+		//$stmt = $db->prepare("INSERT INTO `countries` (`code`, `name`, `group`) VALUES (:code, :name, :group)");
+		$stmt = $db->prepare("UPDATE `countries` SET `name` = :name, `group` = :group WHERE `code` = :code");		
+		$stmt->bindParam(':code', $code , PDO::PARAM_STR, 2);
+		$stmt->bindParam(':name', $name , PDO::PARAM_STR, 50 );
+		$stmt->bindParam(':group', $group , PDO::PARAM_STR, 1 );
+		$stmt->execute();
+		
+		if ( $stmt->rowCount() ) {
+			header('http/1.0 201 Created');
+			
+			$get = $db->prepare("SELECT * FROM countries WHERE code = :code");
+			$get->bindParam(':code', $code, PDO::PARAM_STR, 2);
+			$get->execute();
+			
+			$array = $get->fetch(PDO::FETCH_ASSOC);
+		
+			if ( $type == 'json' ) {
+				header( "content-type: application/json; charset=utf-8" );
+				
+				$array['links']['group'] = $siteurl."countries/".$array['group'].".".$type ;
+				echo json_encode($array);
+			}
+			else {
+				header( "content-type: text/xml; charset=utf-8" );
+				
+				$doc = new DOMDocument('1.0', 'utf-8');
+				$doc->formatOutput = true;
+				
+				$country = $doc->createElement( "country" );
+				$country->setAttribute('code', $array['code']);
+				$country->setAttribute('name', $array['name']);
+				$country->setAttribute('group', $array['group']);
+				$doc->appendChild( $country );
+				
+					$links = $doc->createElement( "links"  );
+					$country->appendChild( $links );
+					
+						$group = $doc->createElement( "group", $siteurl."countries/".$array['group'].".".$type  );
 						$links->appendChild( $group );
 				
 				echo $doc->saveXML();
 		else {
 			header('http/1.0 500 Internal Server Error');
 		}
-
 	 break;	 
 	 
 	case 'HEAD':

File documentation.html

 <html lang="en">
   <head>
     <meta charset="utf-8">
-    <title>Bootstrap, from Twitter</title>
+    <title>EK 2012 api</title>
     <meta name="viewport" content="width=device-width, initial-scale=1.0">
-    <meta name="description" content="">
-    <meta name="author" content="">
+    <meta name="description" content="EK 2012, de teams, groepen en de uitslagen">
+    <meta name="author" content="Remco Raaijmakers">
 
     <!-- Le styles -->
     <link href="css/bootstrap.css" rel="stylesheet">
     <div class="container">
 		
 		<h1><a name="countries"></a>Landen</h1>
-		<div class="well">
-			<dl>
-				<dt><span class="label label-info">GET</span> /countries.(xml|json)</dt>
-				<dd>Haal alle landen op die meespelen</dd>
-			</dl>
-		</div>
+		<h3>Alle landen die meespelen</h3>
+		<pre><span class="label label-info">GET</span> /countries.(xml|json)</pre>
+		<pre><span class="label">Voorbeeld</span> <a href="countries.xml">/countries.xml</a></pre>	
+			
+		<h3>Alle landen van een groep</h3>
+		<p>Mogelijkheden: A, B, C en D</p>
+		<pre><span class="label label-info">GET</span> /countries/[A-D].(xml|json)</pre>
+		<pre><span class="label">Voorbeeld</span> <a href="countries/B.xml">/countries/B.xml</a></pre>		
+	
+		<h3>Informatie van een land</h3>
+		<pre><span class="label label-info">GET</span> /countries/([a-z]{2}).(xml|json)</pre>
+		<pre><span class="label">Voorbeeld</span> <a href="countries/nl.xml">/countries/nl.xml</a></pre>
 
-		<pre><span class="label label-info">GET</span> /countries.(xml|json)
-			<h4>Haal alle landen op die meespelen</h4></pre>
+		<h3>Informatie van een land toevoegen</h3>
+		<pre><span class="label label-important">POST</span> /countries.(xml|json)</pre>
+		<pre><span class="label">Voorbeeld</span> /countries.xml</pre>
+
+	<dl class="dl-horizontal">	
+		<h1><a name="matches"></a>Wedstrijden</h1>
 		
-		<pre><span class="label label-info">GET</span> /countries/([A-D].(xml|json)
-			<h4>Haal alle landen van een groep op. Mogelijkheden: A, B, C en D</h4></pre>	
-	
-		<pre><span class="label label-info">GET</span> /countries/([a-z]{2}).(xml|json)
-			<h4>Haal de informatie op van een land.</h4></pre>	
-	
-GET /countries/([a-z]{2}).(xml|json)
-	De detail info van een land
+		<h3>Informatie van een wedstrijd</h3>
+		<pre><span class="label label-info">GET</span> /matches/([0-9]{2}).(xml|json)</pre>
+		<pre><span class="label">Voorbeeld</span> <a href="matches/12.xml">/matches/12.xml</a></pre>		
 
-GET /matches/{id}
-
-GET /matches.(xml|json)
-	Alle wedstrijden
-
-GET /matches/{wedstrijd nummer}.(xml|json)
-	<dl class="dl-horizontal">	
-		<h1><a name="matches"></a>Landen</h1>
-      
-      <p>Use this document as a way to quick start any new project.<br> All you get is this message and a barebones HTML document.</p>
+		<h3>Alle wedstrijden</h3>
+		<pre><span class="label label-info">GET</span> /matches(xml|json)</pre>
+		<pre><span class="label">Voorbeeld</span> <a href="matches.xml">/matches.xml</a></pre>		
+		
+		<h3>Resultaat bewerken</h3>
+		<pre><span class="label label-success">PUT</span> /matches.xml</pre>
+		<pre><span class="label">Data</span> 
+&lt;match&gt;
+	&lt;id&gt;14&lt;/id&gt;
+	&lt;goals_home&gt;2&lt;/goals_home&gt;
+	&lt;goals_out&gt;1&lt;/goals_out&gt;
+&lt;/match&gt;</pre>		
+		
 
     </div> <!-- /container -->
 
 //ini_set("display_errors",false) ;
 
 $method = $_SERVER['REQUEST_METHOD'];
+$siteurl = "http://localhost/ek-api/";
 
 if ( isset($_GET['id']) && !empty($_GET['id']) && ctype_xdigit($_GET['id']) ) {
 	$id = $_GET['id'];
 									ON m.out = pout.code
 								WHERE m.id = :id ");
 			$stmt->bindParam(':id', $id);
-			
-			// wedstrijd 25 is niet meer in een groep
-			//if ( $id > 24 )
-			
+
 			$stmt->execute();
 			
 			$array = $stmt->fetch(PDO::FETCH_ASSOC);
 			}		
 		}
 		else {
-			$r = $doc->createElement( "matches" );
-			$doc->appendChild( $r );
-		
+	
 			$stmt = $db->query("SELECT m.*, 
 									phome.name AS player_home,
 									pout.name AS player_out,
 			
 				$new_array = Array();
 				foreach($array as $row) {
-					$row['links']['player_home'] = "http://localhost/ek-api/countries/".$row['home'].".".$type ;
-					$row['links']['group_home'] = "http://localhost/ek-api/countries/".$row['group_home'].".".$type ;
-					$row['links']['player_out'] = "http://localhost/ek-api/countries/".$row['out'].".".$type ;
-					$row['links']['group_out'] = "http://localhost/ek-api/countries/".$row['group_out'].".".$type ;					
+					$row['links']['player_home'] = $siteurl."countries/".$row['home'].".".$type ;
+					$row['links']['group_home'] = $siteurl."countries/".$row['group_home'].".".$type ;
+					$row['links']['player_out'] = $siteurl."countries/".$row['out'].".".$type ;
+					$row['links']['group_out'] = $siteurl."countries/".$row['group_out'].".".$type ;					
 					$new_array[] = $row;
 				}
-
+				$new_array = array('matches' => $new_array);
 				echo json_encode($new_array);
-				//echo json_encode($array);
 			}
 			else {
-				//echo '<pre>';
-				//print_r($array);
-				//echo '</pre>';
-				
+				$r = $doc->createElement( "matches" );
+				$doc->appendChild( $r );
+
 				foreach($array as $row) {
 				
 				$match = $doc->createElement( "match" );
 					$links = $doc->createElement( "links"  );
 					$match->appendChild( $links );
 					
-						$player_home = $doc->createElement( "player_home", "http://localhost/ek-api/countries/".$row['home'].".".$type  );
+						$player_home = $doc->createElement( "player_home", $siteurl."countries/".$row['home'].".".$type  );
 						$links->appendChild( $player_home );
 						
-						$group_home = $doc->createElement( "group_home", "http://localhost/ek-api/countries/".$row['group_home'].".".$type  );
+						$group_home = $doc->createElement( "group_home", $siteurl."countries/".$row['group_home'].".".$type  );
 						$links->appendChild( $group_home );
 						
-						$player_out = $doc->createElement( "player_out", "http://localhost/ek-api/countries/".$row['out'].".".$type  );
+						$player_out = $doc->createElement( "player_out", $siteurl."countries/".$row['out'].".".$type  );
 						$links->appendChild( $player_out );
 						
-						$group_out = $doc->createElement( "group_out", "http://localhost/ek-api/countries/".$row['group_out'].".".$type  );
+						$group_out = $doc->createElement( "group_out", $siteurl."countries/".$row['group_out'].".".$type  );
 						$links->appendChild( $group_out );
 
 				}
 		}
 	
 	 break;
-	
-	
-	/* AANPASSEN */
+
 	case 'PUT':		
 	
 		$inputSocket = fopen("php://input", "r");
 		$stmt->bindParam(':goals_out', $goals_out , PDO::PARAM_INT, 2 );
 		$stmt->bindParam(':id', $id , PDO::PARAM_INT, 3 );
 		$stmt->execute();
+		
 		if ( $stmt->rowCount() ) {
 			header('http/1.0 201');
 			
 			if ( $type == 'json' ) {
 				header( "content-type: application/json; charset=utf-8" );
 				// Add links
-				$array['links']['player_home'] = "http://localhost/ek-api/countries/".$array['home'].".".$type ;
-				$array['links']['group_home'] = "http://localhost/ek-api/countries/".$array['group_home'].".".$type ;
-				$array['links']['player_out'] = "http://localhost/ek-api/countries/".$array['out'].".".$type ;
-				$array['links']['group_out'] = "http://localhost/ek-api/countries/".$array['group_out'].".".$type ;
+				$array['links']['player_home'] = $siteurl."countries/".$array['home'].".".$type ;
+				$array['links']['group_home'] = $siteurl."countries/".$array['group_home'].".".$type ;
+				$array['links']['player_out'] = $siteurl."countries/".$array['out'].".".$type ;
+				$array['links']['group_out'] = $siteurl."countries/".$array['group_out'].".".$type ;
 				echo json_encode($array);
 			}
 			else {
 					$links = $doc->createElement( "links"  );
 					$match->appendChild( $links );
 					
-						$player_home = $doc->createElement( "player_home", "http://localhost/ek-api/countries/".$array['home'].".".$type  );
+						$player_home = $doc->createElement( "player_home", $siteurl."countries/".$array['home'].".".$type  );
 						$links->appendChild( $player_home );
 						
-						$group_home = $doc->createElement( "group_home", "http://localhost/ek-api/countries/".$array['group_home'].".".$type  );
+						$group_home = $doc->createElement( "group_home", $siteurl."countries/".$array['group_home'].".".$type  );
 						$links->appendChild( $group_home );
 						
-						$player_out = $doc->createElement( "player_out", "http://localhost/ek-api/countries/".$array['out'].".".$type  );
+						$player_out = $doc->createElement( "player_out", $siteurl."countries/".$array['out'].".".$type  );
 						$links->appendChild( $player_out );
 						
-						$group_out = $doc->createElement( "group_out", "http://localhost/ek-api/countries/".$array['group_out'].".".$type  );
+						$group_out = $doc->createElement( "group_out", $siteurl."countries/".$array['group_out'].".".$type  );
 						$links->appendChild( $group_out );
 				
 				echo $doc->saveXML();
 			header('http/1.0 500 Internal Server Error');
 		}
 	 break;
+
+	case 'DELETE':
+		$stmt = $db->prepare("DELETE FROM `matches`  WHERE `id` = :id");
+		$stmt->bindParam(':id', $id , PDO::PARAM_STR, 2 );
+		$stmt->execute();
+		if ( $stmt->rowCount() ) {
+			header('http/1.0 201');
+		}
+		else {
+			header('http/1.0 500 Internal Server Error');
+		}
+	 break;		 
 	 
 	case 'HEAD':
 		header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1