Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static function http($method, $uri, array $post_data = array(), array $headers = array()) {
- $m = strtoupper($method);
- $u = array_merge(array('path' => '/', 'port' => '80'), parse_url($uri));
- $h = array('host' => $u['host'], 'connection' => 'close');
- if ($m == 'POST') {
- $h += array('content-type' => 'application/x-www-form-urlencoded');
- }
- $h += $headers;
- if (($sock = fsockopen($u['host'], $u['port']))) {
- fputs($sock, $m . ' ' . $u['path'] . ' HTTP/1.1' . "\r\n");
- foreach ($h as $f => $v) {
- fputs($sock, $f . ': ' . $v . "\r\n");
- }
- fputs($sock, "\r\n");
- if ($post_data) {
- fputs($sock, http_build_query($post_data));
- }
- $data = '';
- while (!feof($sock)) {
- $data .= fgets($sock, 4096);
- }
- return implode('', array_slice(explode("\r\n\r\n", $data), 1));
- }
- }
Add Comment
Please, Sign In to add comment