diabliyo

socket io 2.0

Feb 24th, 2012
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.99 KB | None | 0 0
  1. <?php
  2. #
  3. # Socket Lib 2.0
  4. # M.S.I Angel Cantu Jauregui
  5. # Date Feb 24, 2012 22:44
  6. #
  7.  
  8. # obtiene datos por socket, segun peticion POST o GET
  9. # data = array( stream, argumentos, datos_post );
  10. function socket_iodata( $host, $data, $port )
  11.     {
  12.     $r='';
  13.    
  14.     $http_request  = "$data[0] $data[1] HTTP/1.1\r\n";
  15.     if( $data[3] && !strcmp($data[3], "oauth") ) # OAuth
  16.         $http_request .= "User-Agent: Turundus/PHP\r\n";
  17.     $http_request .= "Host: $host\r\n";
  18.     if( !strcmp($data[0], "POST") ) # si es post
  19.         {
  20.         if( $data[3] && !strcmp($data[3], "json") ) # datos json
  21.             $http_request .= "Content-Type: application/json;\r\n";
  22.         if( $data[3] && !strcmp($data[3], "oauth") ) # OAuth
  23.             {
  24.             $http_request .= "Accept: */*\r\n";
  25.             $http_request .= "Authorization: OAuth ". $data[2]. "\r\n";
  26.             }
  27.         else
  28.             $http_request .= "Content-Type: application/x-www-form-urlencoded;\r\n";
  29.            
  30.         if( $data[3] && strcmp($data[3], "oauth") ) # OAuth
  31.             $http_request .= "Content-Length: " . strlen($data[2]) . "\r\n";
  32.         }
  33.     #$http_request .= "User-Agent: reCAPTCHA/PHP\r\n";
  34.     # $http_request .= "Connection: keep-alive\r\n";
  35.     $http_request .= "\r\n";
  36.        
  37.     if( !strcmp($data[0], "POST") ) # si es post
  38.         {
  39.         if( $data[4] && !strcmp($data[3], "oauth") ) # OAuth
  40.                 $http_request .= $data[4];
  41.         else        $http_request .= $data[2];
  42.         }
  43.        
  44.     if( !strcmp($port, "443") )
  45.         $fullhost= 'ssl://'. $host;
  46.     else        $fullhost= $host;
  47.    
  48.     if( ($fs = @fsockopen($fullhost, $port, $errno, $errstr, 10))==FALSE )
  49.         echo 'No se puede abrir socket :: ['. $errno. '] '. $errstr;
  50.     else
  51.         {
  52.         fwrite($fs, $http_request);
  53.  
  54.         while ( !feof($fs) )
  55.             $r .= fgets($fs, 1160); // One TCP-IP packet
  56.         fclose($fs);
  57.         $r= explode("\r\n\r\n", $r, 2);
  58.         return $r;
  59.         }
  60.     }
  61.  
  62. # obtiene datos por socket, segun peticion POST o GET
  63. # data = array( stream, argumentos, datos_post );
  64. function curl_iodata( $host, $data, $port )
  65.     {
  66.     $r='';
  67.     $curl= curl_init($host.$data[1]); # inciamos url
  68.     curl_setopt( $curl, CURLOPT_CUSTOMREQUEST, $data[0] );
  69.     curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
  70.     if( !strcmp($data[0], "POST") ) # si es post
  71.         {
  72.         if( $data[4] && !strcmp($data[3], "oauth") ) # datos oauth
  73.             curl_setopt( $curl, CURLOPT_POSTFIELDS, $data[4] );
  74.         else        curl_setopt( $curl, CURLOPT_POSTFIELDS, $data[2] );
  75.  
  76.         if( $data[3] && !strcmp($data[3], "json") ) # datos json
  77.             $contenido= array( 'Content-Type: application/json', 'Content-Length: '. strlen($data[2]));
  78.         else if( $data[3] && !strcmp($data[3], "oauth") ) # datos oauth
  79.             $contenido= "Authorization: OAuth ". $data[2];
  80.         else        $contenido= array( 'Content-Type: application/x-www-form-urlencoded', 'Content-Length: '. strlen($data[2]));
  81.        
  82.         if( $data[3] && !strcmp($data[3], "oauth") ) # datos oauth
  83.             curl_setopt( $curl, CURLOPT_HTTPHEADER,  array($contenido, "Expect:" ) );
  84.         else
  85.             curl_setopt( $curl, CURLOPT_HTTPHEADER,  $contenido );
  86.         curl_setopt( $curl, CURLOPT_POST, 1 );
  87.         }
  88.     $r= curl_exec($curl);
  89.     curl_close($curl);
  90.     return $r;
  91.     }
  92. ?>
Advertisement
Add Comment
Please, Sign In to add comment