Advertisement
diabliyo

socket io 2.2

Mar 21st, 2012
117
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. # Date Mar 21, 2012 17:21
  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.         else if( $data[3] && !strcmp($data[3], "oauth") ) # OAuth
  23.             {
  24.             $http_request .= "Content-Type: application/x-www-form-urlencoded;\r\n";
  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( !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.     if( !strcmp($port, "443") ) #puerto seguro
  69.         $fullhost= 'https://'. $host;
  70.     else        $fullhost= $host;
  71.     $curl= curl_init($fullhost.$data[1]); # inciamos url
  72.     curl_setopt( $curl, CURLOPT_CUSTOMREQUEST, $data[0] );
  73.     curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
  74.     if( !strcmp($data[0], "POST") ) # si es post
  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= array( 'Content-Type: application/x-www-form-urlencoded', 'User-Agent: Turundus/PHP', 'Accept: */*', 'Authorization: OAuth '. $data[2] );
  80.         else        $contenido= array( 'Content-Type: application/x-www-form-urlencoded', 'Content-Length: '. strlen($data[2]));
  81.  
  82.         if( $data[4] && !strcmp($data[3], "oauth") ) # datos oauth
  83.             {
  84.             $contenido[]= 'Content-Length: '. strlen($data[4]);
  85.             curl_setopt( $curl, CURLOPT_POSTFIELDS, $data[4] );
  86.             }
  87.         else if( !$data[4] && !strcmp($data[3], "oauth") ) # datos oauth
  88.             {
  89.             $contenido[]= 'Content-Length: 0';
  90.             curl_setopt( $curl, CURLOPT_POSTFIELDS, $data[4] );
  91.             }  
  92.         else
  93.             curl_setopt( $curl, CURLOPT_POSTFIELDS, $data[2] );
  94.        
  95.         curl_setopt( $curl, CURLOPT_HTTPHEADER,  $contenido );
  96.         curl_setopt( $curl, CURLOPT_POST, 1 );
  97.         # curl_setopt(CURL_VERBOSE, 1);
  98.         }
  99.  
  100.     $r= curl_exec($curl);
  101.     curl_close($curl);
  102.     return $r;
  103.     }
  104.  
  105. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement