Problem with Developer Api
Posted: Mon Jul 16, 2012 11:57 am
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:
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){ } } ?>