Advertisement
diabliyo

socket io 2.0

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