Advertisement
diabliyo

socket io 2.2

Mar 21st, 2012
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.40 KB | None | 0 0
  1. <?php
  2. #
  3. # Socket Lib 2.2
  4. # M.S.I Angel Cantu Jauregui
  5. # angel.cantu@sie-group.net
  6. # Date Mar 21, 2012 17:21
  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.         else if( $data[3] && !strcmp($data[3], "oauth") ) # OAuth
  24.             {
  25.             $http_request .= "Content-Type: application/x-www-form-urlencoded;\r\n";
  26.             $http_request .= "Accept: */*\r\n";
  27.             $http_request .= "Authorization: OAuth ". $data[2]. "\r\n";
  28.             }
  29.         else
  30.             $http_request .= "Content-Type: application/x-www-form-urlencoded;\r\n";
  31.            
  32.         if( $data[3] && strcmp($data[3], "oauth") ) # OAuth
  33.             $http_request .= "Content-Length: " . strlen($data[2]) . "\r\n";
  34.         }
  35.     # $http_request .= "User-Agent: reCAPTCHA/PHP\r\n";
  36.     # $http_request .= "Connection: keep-alive\r\n";
  37.     $http_request .= "\r\n";
  38.        
  39.     if( !strcmp($data[0], "POST") ) # si es post
  40.         {
  41.         if( !strcmp($data[3], "oauth") ) # OAuth
  42.                 $http_request .= $data[4];
  43.         else        $http_request .= $data[2];
  44.         }
  45.        
  46.     if( !strcmp($port, "443") )
  47.         $fullhost= 'ssl://'. $host;
  48.     else        $fullhost= $host;
  49.    
  50.     if( ($fs = @fsockopen($fullhost, $port, $errno, $errstr, 10))==FALSE )
  51.         echo 'No se puede abrir socket :: ['. $errno. '] '. $errstr;
  52.     else
  53.         {
  54.         fwrite($fs, $http_request);
  55.  
  56.         while ( !feof($fs) )
  57.             $r .= fgets($fs, 1160); // One TCP-IP packet
  58.         fclose($fs);
  59.         $r= explode("\r\n\r\n", $r, 2);
  60.         return $r;
  61.         }
  62.     }
  63.  
  64. # obtiene datos por socket, segun peticion POST o GET
  65. # data = array( stream, argumentos, datos_post );
  66. function curl_iodata( $host, $data, $port )
  67.     {
  68.     $r='';
  69.     if( !strcmp($port, "443") ) #puerto seguro
  70.         $fullhost= 'https://'. $host;
  71.     else        $fullhost= $host;
  72.     $curl= curl_init($fullhost.$data[1]); # inciamos url
  73.     curl_setopt( $curl, CURLOPT_CUSTOMREQUEST, $data[0] );
  74.     curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
  75.     if( !strcmp($data[0], "POST") ) # si es post
  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= array( 'Content-Type: application/x-www-form-urlencoded', 'User-Agent: Turundus/PHP', 'Accept: */*', 'Authorization: OAuth '. $data[2] );
  81.         else        $contenido= array( 'Content-Type: application/x-www-form-urlencoded', 'Content-Length: '. strlen($data[2]));
  82.  
  83.         if( $data[4] && !strcmp($data[3], "oauth") ) # datos oauth
  84.             {
  85.             $contenido[]= 'Content-Length: '. strlen($data[4]);
  86.             curl_setopt( $curl, CURLOPT_POSTFIELDS, $data[4] );
  87.             }
  88.         else if( !$data[4] && !strcmp($data[3], "oauth") ) # datos oauth
  89.             {
  90.             $contenido[]= 'Content-Length: 0';
  91.             curl_setopt( $curl, CURLOPT_POSTFIELDS, $data[4] );
  92.             }  
  93.         else
  94.             curl_setopt( $curl, CURLOPT_POSTFIELDS, $data[2] );
  95.        
  96.         curl_setopt( $curl, CURLOPT_HTTPHEADER,  $contenido );
  97.         curl_setopt( $curl, CURLOPT_POST, 1 );
  98.         # curl_setopt(CURL_VERBOSE, 1);
  99.         }
  100.  
  101.     $r= curl_exec($curl);
  102.     curl_close($curl);
  103.     return $r;
  104.     }
  105.  
  106. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement