Guest User

Untitled

a guest
Nov 20th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.94 KB | None | 0 0
  1. public static function http($method, $uri, array $post_data = array(), array $headers = array()) {
  2.  
  3.     $m = strtoupper($method);
  4.     $u = array_merge(array('path' => '/', 'port' => '80'), parse_url($uri));
  5.     $h = array('host' => $u['host'], 'connection' => 'close');
  6.  
  7.     if ($m == 'POST') {
  8.         $h += array('content-type' => 'application/x-www-form-urlencoded');
  9.     }
  10.  
  11.     $h += $headers;
  12.  
  13.     if (($sock = fsockopen($u['host'], $u['port']))) {
  14.  
  15.         fputs($sock, $m . ' ' . $u['path'] . ' HTTP/1.1' . "\r\n");
  16.  
  17.         foreach ($h as $f => $v) {
  18.             fputs($sock, $f . ': ' . $v . "\r\n");
  19.         }
  20.  
  21.         fputs($sock, "\r\n");
  22.  
  23.         if ($post_data) {
  24.             fputs($sock, http_build_query($post_data));
  25.         }
  26.  
  27.         $data = '';
  28.         while (!feof($sock)) {
  29.             $data .= fgets($sock, 4096);
  30.         }
  31.  
  32.         return implode('', array_slice(explode("\r\n\r\n", $data), 1));
  33.     }
  34. }
Add Comment
Please, Sign In to add comment