note 101872 modified in function.fsockopen by danbrown

From: Date: Fri, 24 Jun 2011 15:25:52 +0000
Subject: note 101872 modified in function.fsockopen by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-180916@lists.php.net to get a copy of this message
Here is my fsockopen-based HTTP request fonction (GET and POST) : <?php function http_request( $verb = 'GET', /* HTTP Request Method (GET and POST supported) */ $ip, /* Target IP/Hostname */ $port = 80, /* Target TCP port */ $uri = '/', /* Target URI */ $getdata = array(), /* HTTP GET Data ie. array('var1' => 'val1', 'var2' => 'val2') */ $postdata = array(), /* HTTP POST Data ie. array('var1' => 'val1', 'var2' => 'val2') */ $cookie = array(), /* HTTP Cookie Data ie. array('var1' => 'val1', 'var2' => 'val2') */ $custom_headers = array(), /* Custom HTTP headers ie. array('Referer: http://localhost/ */ $timeout = 1000, /* Socket timeout in milliseconds */ $req_hdr = false, /* Include HTTP request headers */ $res_hdr = false /* Include HTTP response headers */ ) { $ret = ''; $verb = strtoupper($verb); $cookie_str = ''; $getdata_str = count($getdata) ? '?' : ''; $postdata_str = ''; foreach ($getdata as $k => $v) $getdata_str .= urlencode($k) .'='. urlencode($v) . '&'; foreach ($postdata as $k => $v) $postdata_str .= urlencode($k) .'='. urlencode($v) .'&'; foreach ($cookie as $k => $v) $cookie_str .= urlencode($k) .'='. urlencode($v) .'; '; $crlf = "\r\n"; $req = $verb .' '. $uri . $getdata_str .' HTTP/1.1' . $crlf; $req .= 'Host: '. $ip . $crlf; $req .= 'User-Agent: Mozilla/5.0 Firefox/3.6.12' . $crlf; $req .= 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' . $crlf; $req .= 'Accept-Language: en-us,en;q=0.5' . $crlf; $req .= 'Accept-Encoding: deflate' . $crlf; $req .= 'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7' . $crlf; foreach ($custom_headers as $k => $v) $req .= $k .': '. $v . $crlf; if (!empty($cookie_str)) $req .= 'Cookie: '. substr($cookie_str, 0, -2) . $crlf; if ($verb == 'POST' && !empty($postdata_str)) { $postdata_str = substr($postdata_str, 0, -1); $req .= 'Content-Type: application/x-www-form-urlencoded' . $crlf; $req .= 'Content-Length: '. strlen($postdata_str) . $crlf . $crlf; $req .= $postdata_str; } else $req .= $crlf; if ($req_hdr) $ret .= $req; if (($fp = @fsockopen($ip, $port, $errno, $errstr)) == false) return "Error $errno: $errstr\n"; stream_set_timeout($fp, 0, $timeout * 1000); fputs($fp, $req); while ($line = fgets($fp)) $ret .= $line; fclose($fp); if (!$res_hdr) $ret = substr($ret, strpos($ret, "\r\n\r\n") + 4); return $ret; } ?> Example usages : <?php echo http_request('GET', 'www.php.net'); echo http_request('GET', 'www.php.net', 80, '/manual/en/function.phpinfo.php'); echo http_request('GET', 'www.php.net', 80, '/manual/en/function.phpinfo.php', array('get1' => 'v_get1'), array(), array('cookie1' => 'v_cookie1'), array('X-My-Header' => 'My Value')); ?> [EDIT BY danbrown AT php DOT net: Contains a bugfix provided by "Wrinkled Cheese" on 24-JUN-2011 to fix the $getdata foreach() loop.] --was-- Here is my fsockopen-based HTTP request fonction (GET and POST) : <?php function http_request( $verb = 'GET', /* HTTP Request Method (GET and POST supported) */ $ip, /* Target IP/Hostname */ $port = 80, /* Target TCP port */ $uri = '/', /* Target URI */ $getdata = array(), /* HTTP GET Data ie. array('var1' => 'val1', 'var2' => 'val2') */ $postdata = array(), /* HTTP POST Data ie. array('var1' => 'val1', 'var2' => 'val2') */ $cookie = array(), /* HTTP Cookie Data ie. array('var1' => 'val1', 'var2' => 'val2') */ $custom_headers = array(), /* Custom HTTP headers ie. array('Referer: http://localhost/ */ $timeout = 1000, /* Socket timeout in milliseconds */ $req_hdr = false, /* Include HTTP request headers */ $res_hdr = false /* Include HTTP response headers */ ) { $ret = ''; $verb = strtoupper($verb); $cookie_str = ''; $getdata_str = count($getdata) ? '?' : ''; $postdata_str = ''; foreach ($getdata as $k => $v) $getdata_str .= urlencode($k) .'='. urlencode($v) . '&'; foreach ($postdata as $k => $v) $postdata_str .= urlencode($k) .'='. urlencode($v) .'&'; foreach ($cookie as $k => $v) $cookie_str .= urlencode($k) .'='. urlencode($v) .'; '; $crlf = "\r\n"; $req = $verb .' '. $uri . $getdata_str .' HTTP/1.1' . $crlf; $req .= 'Host: '. $ip . $crlf; $req .= 'User-Agent: Mozilla/5.0 Firefox/3.6.12' . $crlf; $req .= 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' . $crlf; $req .= 'Accept-Language: en-us,en;q=0.5' . $crlf; $req .= 'Accept-Encoding: deflate' . $crlf; $req .= 'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7' . $crlf; foreach ($custom_headers as $k => $v) $req .= $k .': '. $v . $crlf; if (!empty($cookie_str)) $req .= 'Cookie: '. substr($cookie_str, 0, -2) . $crlf; if ($verb == 'POST' && !empty($postdata_str)) { $postdata_str = substr($postdata_str, 0, -1); $req .= 'Content-Type: application/x-www-form-urlencoded' . $crlf; $req .= 'Content-Length: '. strlen($postdata_str) . $crlf . $crlf; $req .= $postdata_str; } else $req .= $crlf; if ($req_hdr) $ret .= $req; if (($fp = @fsockopen($ip, $port, $errno, $errstr)) == false) return "Error $errno: $errstr\n"; stream_set_timeout($fp, 0, $timeout * 1000); fputs($fp, $req); while ($line = fgets($fp)) $ret .= $line; fclose($fp); if (!$res_hdr) $ret = substr($ret, strpos($ret, "\r\n\r\n") + 4); return $ret; } ?> Example usages : <?php echo http_request('GET', 'www.php.net'); echo http_request('GET', 'www.php.net', 80, '/manual/en/function.phpinfo.php'); echo http_request('GET', 'www.php.net', 80, '/manual/en/function.phpinfo.php', array('get1' => 'v_get1'), array(), array('cookie1' => 'v_cookie1'), array('X-My-Header' => 'My Value')); ?> http://php.net/manual/en/function.fsockopen.php

« previous php.notes (#180916) next »