pjg / wp_movie_ratings

WP Movie Ratings is a wordpress plugin for rating movies.

This URL has Read+Write access

wp_movie_ratings / wp_http_request.class.php
100644 95 lines (71 sloc) 2.353 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
 
# WP_HTTP_Request PHP class by taken from php.net comments
# http://php.net/manual/en/function.fopen.php#58099
# Author: info AT b1g.de
 
# Usage:
# $r = new WP_HTTP_Request('http://www.php.net');
# echo $r->DownloadToString();
 
class WP_HTTP_Request {
  var $_fp; # HTTP socket
  var $_url; # Full URL
  var $_host; # HTTP host
  var $_protocol; # Protocol (HTTP/HTTPS)
  var $_uri; # Request URI
  var $_port; # Port
 
  # scan url
  function _scan_url() {
    $req = $this->_url;
 
    $pos = strpos($req, '://');
    $this->_protocol = strtolower(substr($req, 0, $pos));
 
    $req = substr($req, $pos+3);
    $pos = strpos($req, '/');
 
    if ($pos === false) $pos = strlen($req);
 
    $host = substr($req, 0, $pos);
 
    if (strpos($host, ':') !== false) {
      list($this->_host, $this->_port) = explode(':', $host);
    } else {
      $this->_host = $host;
      $this->_port = ($this->_protocol == 'https') ? 443 : 80;
    }
 
    $this->_uri = substr($req, $pos);
 
    if ($this->_uri == '') $this->_uri = '/';
  }
 
  # constructor
  function WP_HTTP_Request($url) {
    $this->_url = $url;
    $this->_scan_url();
  }
 
  # download URL to string
  function DownloadToString() {
    $crlf = "\r\n";
 
    # generate request
    $req = 'GET ' . $this->_uri . ' HTTP/1.0' . $crlf . 'Host: ' . $this->_host . $crlf . $crlf;
 
    # fetch
    $response = '';
    $this->_fp = fsockopen(($this->_protocol == 'https' ? 'ssl://' : '') . $this->_host, $this->_port);
    fwrite($this->_fp, $req);
 
    while(is_resource($this->_fp) && $this->_fp && !feof($this->_fp))
      $response .= fread($this->_fp, 1024);
 
    fclose($this->_fp);
 
    # split header and body
    $pos = strpos($response, $crlf . $crlf);
 
    if ($pos === false) return($response);
 
    $header = substr($response, 0, $pos);
    $body = substr($response, $pos + 2 * strlen($crlf));
 
    # parse headers
    $headers = array();
    $lines = explode($crlf, $header);
 
    foreach ($lines as $line)
      if (($pos = strpos($line, ':')) !== false)
        $headers[strtolower(trim(substr($line, 0, $pos)))] = trim(substr($line, $pos+1));
 
    # redirection?
    if (isset($headers['location'])) {
      $http = new WP_HTTP_Request($headers['location']);
      return $http->DownloadToString($http);
    }
    else {
      return($body);
    }
  }
}
?>