Problem with Developer Api

Post here is you are having problems with any of the tutorials.
Post Reply
comz123
Posts: 1
Joined: Mon Jul 16, 2012 11:47 am

Problem with Developer Api

Post by comz123 »

I followed the tutorial exactly, or so I think.

In the last step I have a problem. The header is not displaying right "Array ( [0] => HTTP/1.1 200 OK Server: nginx admin Date: Mon, 16 Jul 2012 11:41:44 GMT Content-Type: application/json Transfer-Encoding: chunked Connection: close X-Powered-By: PHP/5.3.10 [1] => 17 {"url":false,"key":"0"} 0 [2] => )"

is what it is showing when it shouldn't
My Code:
<?

class url_shortener {

	const API_URL = 'http://exr.me/api.php';
	
	// Internal Function For Sending Post Data
	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.1\r\n";
		}else{
			$head = "POST {$url['path']} HTTP/1.1\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}");
		
		print_r(explode("\r\n\r\n", stream_get_contents($socket)));
	}
	
	// Dis Gets Dat Url For De Given Key
	public static function lookup($key){
		self::send_post_data(array('lookup' => $key));
		
	}
	
	// Shortens Dat Url
	public static function shorten($url){
		
	}
	
}

?>
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Problem with Developer Api

Post by jacek »

That looks right to me :? what makes you think it's wrong ?
Image
Post Reply