<?php
class url_shortener {
		
	const API_URL = 'http://localhost:8888/r2/api/a.php';
	
	private static function send_post_data($data) {
		
		$url 		= parse_url(self::API_URL);
		$boundary 	= md5(microtime(true));
		
		$post = '';
		
		foreach ($data as $name => $value) {
			$post .= "--{$boundary}\r\n";
			$post .= "Content-Disposition: form-data; name=\"{$name}\"\r\n\r\n";
			$post .= $value . "\r\n";
		}
		
		$post .= "--{$boundary}--\r\n";
		
		if (isset($url['query'])) {
			$head = "POST = {$url['path']}?{$url['query']} HTTP/1.0\r\n";
		} else {
			$head = "POST = {$url['path']} HTTP/1.0\r\n";
		}
		
		$head .= "Host: {$url['host']}\r\n";
		$head .= "Content-Type: multipart/form-data; boundary=\"{$boundary}\"\r\n";
		$head .= "Content-Length: " . strlen($post) . "\r\n";
		$head .= "Connection: close\r\n\r\n";
		
		$socket = fsockopen($url['host'], ((isset($url['port'])) ? $url['port'] : 80));
		
		fwrite($socket, "{$head}{$post}");
		
		echo end(explode("\r\n\r\n", stream_get_contents($socket)));
	}
	
	// gets the url for the key
	public static function lookup($key) {
		self::send_post_data(array(
			'lookup'	=> $key,
		));
	}
	
	// shortens a new url
	public static function shorten($url) {
		
	}
		
}
?>
the error im getting isand error code 400
Bad Request
Your browser sent a request that this server could not understand.



