Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- #
- # Socket Lib 2.2
- # M.S.I Angel Cantu Jauregui
- # angel.cantu@sie-group.net
- # Date Mar 21, 2012 17:21
- #
- # obtiene datos por socket, segun peticion POST o GET
- # data = array( stream, argumentos, datos_post );
- function socket_iodata( $host, $data, $port )
- {
- $r='';
- $http_request = "$data[0] $data[1] HTTP/1.1\r\n";
- if( $data[3] && !strcmp($data[3], "oauth") ) # OAuth
- $http_request .= "User-Agent: Turundus/PHP\r\n";
- $http_request .= "Host: $host\r\n";
- if( !strcmp($data[0], "POST") ) # si es post
- {
- if( $data[3] && !strcmp($data[3], "json") ) # datos json
- $http_request .= "Content-Type: application/json;\r\n";
- else if( $data[3] && !strcmp($data[3], "oauth") ) # OAuth
- {
- $http_request .= "Content-Type: application/x-www-form-urlencoded;\r\n";
- $http_request .= "Accept: */*\r\n";
- $http_request .= "Authorization: OAuth ". $data[2]. "\r\n";
- }
- else
- $http_request .= "Content-Type: application/x-www-form-urlencoded;\r\n";
- if( $data[3] && strcmp($data[3], "oauth") ) # OAuth
- $http_request .= "Content-Length: " . strlen($data[2]) . "\r\n";
- }
- # $http_request .= "User-Agent: reCAPTCHA/PHP\r\n";
- # $http_request .= "Connection: keep-alive\r\n";
- $http_request .= "\r\n";
- if( !strcmp($data[0], "POST") ) # si es post
- {
- if( !strcmp($data[3], "oauth") ) # OAuth
- $http_request .= $data[4];
- else $http_request .= $data[2];
- }
- if( !strcmp($port, "443") )
- $fullhost= 'ssl://'. $host;
- else $fullhost= $host;
- if( ($fs = @fsockopen($fullhost, $port, $errno, $errstr, 10))==FALSE )
- echo 'No se puede abrir socket :: ['. $errno. '] '. $errstr;
- else
- {
- fwrite($fs, $http_request);
- while ( !feof($fs) )
- $r .= fgets($fs, 1160); // One TCP-IP packet
- fclose($fs);
- $r= explode("\r\n\r\n", $r, 2);
- return $r;
- }
- }
- # obtiene datos por socket, segun peticion POST o GET
- # data = array( stream, argumentos, datos_post );
- function curl_iodata( $host, $data, $port )
- {
- $r='';
- if( !strcmp($port, "443") ) #puerto seguro
- $fullhost= 'https://'. $host;
- else $fullhost= $host;
- $curl= curl_init($fullhost.$data[1]); # inciamos url
- curl_setopt( $curl, CURLOPT_CUSTOMREQUEST, $data[0] );
- curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
- if( !strcmp($data[0], "POST") ) # si es post
- {
- if( $data[3] && !strcmp($data[3], "json") ) # datos json
- $contenido= array( 'Content-Type: application/json', 'Content-Length: '. strlen($data[2]));
- else if( $data[3] && !strcmp($data[3], "oauth") ) # datos oauth
- $contenido= array( 'Content-Type: application/x-www-form-urlencoded', 'User-Agent: Turundus/PHP', 'Accept: */*', 'Authorization: OAuth '. $data[2] );
- else $contenido= array( 'Content-Type: application/x-www-form-urlencoded', 'Content-Length: '. strlen($data[2]));
- if( $data[4] && !strcmp($data[3], "oauth") ) # datos oauth
- {
- $contenido[]= 'Content-Length: '. strlen($data[4]);
- curl_setopt( $curl, CURLOPT_POSTFIELDS, $data[4] );
- }
- else if( !$data[4] && !strcmp($data[3], "oauth") ) # datos oauth
- {
- $contenido[]= 'Content-Length: 0';
- curl_setopt( $curl, CURLOPT_POSTFIELDS, $data[4] );
- }
- else
- curl_setopt( $curl, CURLOPT_POSTFIELDS, $data[2] );
- curl_setopt( $curl, CURLOPT_HTTPHEADER, $contenido );
- curl_setopt( $curl, CURLOPT_POST, 1 );
- # curl_setopt(CURL_VERBOSE, 1);
- }
- $r= curl_exec($curl);
- curl_close($curl);
- return $r;
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement