Commits

Anonymous committed defe385

First GET results of countries.php

Comments (0)

Files changed (4)

counties.php

-<?php
-//ini_set("display_errors",false) ;
-
-$method = $_SERVER['REQUEST_METHOD'];
-
-if ( isset($_GET['shortcode']) && !empty($_GET['shortcode']) ) {
-	$shortcode = $_GET['shortcode'];
-}
-else {
-	$shortcode = 'undefined';
-}
-
-if ( isset($_GET['type']) && !empty($_GET['type']) ) {
-	$type = $_GET['type'];
-}
-else {
-	$type = 'xml';
-}
-
-switch ( $method ) {
-	case 'GET':
-
-		switch( $type ) {
-			case 'xml':
-				header( "content-type: text/xml; charset=utf-8" );
-				echo '<country><id>1234</id></country>';
-			 break;
-		}
-	
-	 break;
-	
-	case 'PUT':
-		if ( isset($_GET['id']) && !empty($_GET['id']) && ctype_xdigit($_GET['id']) ) {
-			$id = $_GET['id'];
-		}	
-		
-		$inputSocket = fopen("php://input", "r");
-		$inputData = '';
-		
-		while ($data = fread($inputSocket, 1024) ) {
-			$inputData .= $data;
-		}
-		fclose($inputSocket);
-		
-		//$readData = simplexml_load_string($inputData);
-		if ( file_exists('reviews/'.$id.'.xml') ) {
-			$isNewFile = true;
-		} else {
-			$isNewFile = false;
-		}
-		// schrijf bestand.
-		$handle = fopen('reviews/'.$id.'.xml', 'w');
-		if ( fwrite($handle, $inputData) ) {
-			if ($isNewFile) {
-				header('http/1.0 201 Created');
-			} else {
-				header('http/1.0 204');
-			}
-			
-			header( "content-type: text/xml; charset=utf-8" );
-			echo '<detailUrls><xml>http://localhost/str6/reviews/'.$id.'.xml</xml><html>http://localhost/str6/reviews/'.$id.'.html</html><detailUrls>';	
-		}
-		else {
-			header('http/1.0 500 Internal Server Error');
-		}
-		fclose($handle);			
-	 break;
-	 
-	case 'HEAD':
-		header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
-		header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
-	 break;
-	 
-	case 'OPTIONS':
-		header('Allow: OPTIONS, HEAD, GET, PUT');
-	 break;
-}
-?>
+<?php
+include('database.php');
+//ini_set("display_errors",false) ;
+
+$method = $_SERVER['REQUEST_METHOD'];
+
+if ( isset($_GET['shortcode']) && !empty($_GET['shortcode']) ) {
+	$shortcode = $_GET['shortcode'];
+}
+else {
+	$shortcode = false;
+}
+
+if ( isset($_GET['type']) && !empty($_GET['type']) ) {
+	$type = $_GET['type'];
+}
+else {
+	$type = 'xml';
+}
+
+switch ( $method ) {
+	case 'GET':
+		header( "content-type: text/xml; charset=utf-8" );
+		
+		$doc = new DOMDocument('1.0', 'utf-8');
+		$doc->formatOutput = true;
+		$r = $doc->createElement( "countries" );
+		$doc->appendChild( $r );
+				
+		if  ( $shortcode ) {
+			$stmt = $db->prepare("SELECT * FROM countries WHERE code = :code");
+			$stmt->bindParam(':code', $shortcode, PDO::PARAM_STR, 3);
+			
+			$stmt->execute();
+			
+			$obj = $stmt->fetch(PDO::FETCH_OBJ);
+			
+			$country = $doc->createElement( "country" );
+			$country->setAttribute('code', $obj->code);
+			$r->appendChild( $country );
+			
+				$name = $doc->createElement( "name", $obj->name  );
+				$country->appendChild( $name );	
+			
+				$group = $doc->createElement( "group", $obj->group  );
+				$country->appendChild( $group );				
+		}
+		else {
+			
+			foreach($db->query('SELECT * FROM countries') as $row) {
+			
+			$country = $doc->createElement( "country" );
+			$country->setAttribute('code', $row['code']);
+			$r->appendChild( $country );
+			
+				$name = $doc->createElement( "name", $row['name']  );
+				$country->appendChild( $name );	
+			
+				$group = $doc->createElement( "group", $row['group']  );
+				$country->appendChild( $group );			
+			}
+			
+		}	
+		echo $doc->saveXML();
+	 break;
+	
+	case 'PUT':		
+	/*
+		$inputSocket = fopen("php://input", "r");
+		$inputData = '';
+		
+		while ($data = fread($inputSocket, 1024) ) {
+			$inputData .= $data;
+		}
+		fclose($inputSocket);
+		
+		//$readData = simplexml_load_string($inputData);
+		if ( file_exists('reviews/'.$id.'.xml') ) {
+			$isNewFile = true;
+		} else {
+			$isNewFile = false;
+		}
+		// schrijf bestand.
+		$handle = fopen('reviews/'.$id.'.xml', 'w');
+		if ( fwrite($handle, $inputData) ) {
+			if ($isNewFile) {
+				header('http/1.0 201 Created');
+			} else {
+				header('http/1.0 204');
+			}
+			
+			header( "content-type: text/xml; charset=utf-8" );
+			echo '<detailUrls><xml>http://localhost/str6/reviews/'.$id.'.xml</xml><html>http://localhost/str6/reviews/'.$id.'.html</html><detailUrls>';	
+		}
+		else {
+			header('http/1.0 500 Internal Server Error');
+		}
+		fclose($handle);	*/		
+	 break;
+	 
+	case 'HEAD':
+		header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
+		header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
+	 break;
+	 
+	case 'OPTIONS':
+		header('Allow: OPTIONS, HEAD, GET, PUT');
+	 break;
+}
+?>
+<?php
+try {
+    $db = new PDO("mysql:dbname=ekapi;host=localhost", "root", "" );
+    }
+catch(PDOException $e)
+    {
+    echo $e->getMessage();
+    }
+?>
 <?php
+include('database.php');
 //ini_set("display_errors",false) ;
 
 $method = $_SERVER['REQUEST_METHOD'];