Advertisement
diabliyo

sockets_io

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