Source Code for Me (s-c.me)

Allows you to paste souce code to blogs! Adapted for Twitter! Here is Search Form in case you missed your code.
Code:
Selected Language:
Show Linenumbers:
Short link for Twitter:
HTML:

HTML view:

Copy Source | Copy HTML
  1. // sockets version HTTP/POST
  2. function http_post( $url, $data ) {
  3.     $eol = "\r\n";
  4.     $post = '';
  5.     if (is_array($data)) {
  6.         foreach( $data as $k => $v)
  7.             $post .= $k.'='.urlencode($v).'&';
  8.         $post = substr($post, 0,-1);
  9.         $content_type = 'application/x-www-form-urlencoded';
  10.     } else {
  11.         $post = $data;
  12.         if (strpos($post, '<?xml') ===  0)
  13.             $content_type = 'text/xml';
  14.         else if (strpos($post, '{') ===  0)
  15.             $content_type = 'application/json';
  16.         else
  17.             $content_type = 'text/html';
  18.     }
  19.     if ((($u = parse_url($url)) === false) || !isset($u['host'])) return false;
  20.     if (!isset($u['scheme'])) $u['scheme'] = 'http';
  21.     $request = 'POST '.(isset($u['path']) ? $u['path'] : '/').((isset($u['query'])) ? '?'.$u['query'] : '' ).' HTTP/1.1'.$eol
  22.         .'Host: '.$u['host'].$eol
  23.         .'Content-Type: '.$content_type.$eol
  24.         .'Content-Length: '.mb_strlen($post, 'latin1').$eol
  25.         .'Connection: close'.$eol.$eol
  26.         .$post;
  27.     $host = ($u['scheme'] == 'https') ? 'ssl://'.$u['host'] : $u['host'];
  28.     if (isset($u['port']))
  29.         $port = $u['port'];
  30.     else
  31.         $port = ($u['scheme'] == 'https') ? 443 : 80;
  32.     $fp = @fsockopen( $host, $port, $errno, $errstr, 10);
  33.     if ($fp) {
  34.         $content = '';
  35.         $content_length = false;
  36.         $chunked = false;
  37.         fwrite($fp, $request);
  38.         // read headers                
  39.         while ($line = fgets($fp)) {
  40.             if (preg_match('~Content-Length: (\d+)~i', $line, $matches)) {
  41.                 $content_length = (int) $matches[1];
  42.             } else if (preg_match('~Transfer-Encoding: chunked~i', $line)) {
  43.                 $chunked = true;
  44.             } else if ($line == "\r\n") {
  45.                 break;
  46.             }
  47.         }
  48.         // read content        
  49.         if ($content_length !== false) {
  50.             $_size = 4096;
  51.             do {
  52.                 $_data = fread($fp, $_size );
  53.                 $content .= $_data;
  54.                 $_size = min($content_length-strlen($content), 4096);
  55.             } while( $_size >  0 );
  56.         } else if ($chunked) {
  57.             while ( $chunk_length = hexdec(trim(fgets($fp))) ) {
  58.                 $chunk = '';
  59.                 $read_length =  0;
  60.                 while ( $read_length < $chunk_length ) {
  61.                     $chunk .= fread($fp, $chunk_length - $read_length);
  62.                     $read_length = strlen($chunk);
  63.                 }
  64.                 $content .= $chunk;
  65.                 fgets($fp);
  66.             }
  67.         } else {
  68.             while(!feof($fp)) $content .= fread($fp, 4096);
  69.         }
  70.         fclose($fp);
  71. //        echo $content;
  72.         return $content;
  73.     } else {
  74.         return false;
  75.     }
  76. }




Based on Manoli.Net's CodeFormatter. Made by Topbot (c) 2008-2012