1 | <?php |
---|
2 | /* |
---|
3 | * http_request class |
---|
4 | * |
---|
5 | * useful to avoid allow_url_fopen_wrapper issues |
---|
6 | * and to get or post data from anywhere we want |
---|
7 | * |
---|
8 | */ |
---|
9 | |
---|
10 | class http_request |
---|
11 | { |
---|
12 | function http_request($url = '', $method = 'GET') |
---|
13 | { |
---|
14 | if ($url) |
---|
15 | { |
---|
16 | $this->url = parse_url($url); |
---|
17 | } |
---|
18 | $this->useragent = 'Mozilla/4.0 (compatible)'; |
---|
19 | $this->method = $method; |
---|
20 | $this->postform = array(); |
---|
21 | $this->postdata = array(); |
---|
22 | } |
---|
23 | |
---|
24 | function get_content() |
---|
25 | { |
---|
26 | $this->request(); |
---|
27 | return $this->content; |
---|
28 | } |
---|
29 | |
---|
30 | function get_header() |
---|
31 | { |
---|
32 | $this->request(); |
---|
33 | return $this->header; |
---|
34 | } |
---|
35 | |
---|
36 | function get_sent() |
---|
37 | { |
---|
38 | return $this->sent; |
---|
39 | } |
---|
40 | |
---|
41 | function get_recv() |
---|
42 | { |
---|
43 | return $this->recv; |
---|
44 | } |
---|
45 | |
---|
46 | function connect() |
---|
47 | { |
---|
48 | if ($this->fp) |
---|
49 | { |
---|
50 | return true; |
---|
51 | } |
---|
52 | |
---|
53 | $this->fp = fsockopen($this->url["host"], 80, $errno, $errstr, 10); |
---|
54 | if (!$this->fp) |
---|
55 | { |
---|
56 | echo "Error occured with fsockopen: $errstr ($errno)<br>\n"; |
---|
57 | return false; |
---|
58 | } |
---|
59 | return true; |
---|
60 | } |
---|
61 | |
---|
62 | function request() |
---|
63 | { |
---|
64 | if ($this->requested) |
---|
65 | { |
---|
66 | return; |
---|
67 | } |
---|
68 | $this->connect(); |
---|
69 | $fp = &$this->fp; |
---|
70 | |
---|
71 | if (!is_resource($fp)) |
---|
72 | { |
---|
73 | return false; |
---|
74 | } |
---|
75 | |
---|
76 | // define a linefeed (carriage return + newline) |
---|
77 | $lf = "\r\n"; |
---|
78 | |
---|
79 | $request_string = $this->method.' '.$this->url['path'].'?'.$this->url['query'].' HTTP/1.0'.$lf |
---|
80 | .'Accept-Language: en'.$lf |
---|
81 | .'User-Agent: '.$this->useragent.$lf |
---|
82 | .'Host: '.$this->url['host'].$lf |
---|
83 | .'Connection: close'.$lf; |
---|
84 | if ($this->method == 'POST') |
---|
85 | { |
---|
86 | $boundary = substr(md5(rand(0,32000)),0,10); |
---|
87 | $data = '--'.$boundary.$lf; |
---|
88 | |
---|
89 | foreach ($this->postform as $name => $content_file) |
---|
90 | { |
---|
91 | $data .= 'Content-Disposition: form-data; name="'.$name.'"'.$lf.$lf; |
---|
92 | $data .= $content_file.$lf; |
---|
93 | $data .= '--'.$boundary.$lf; |
---|
94 | } |
---|
95 | |
---|
96 | foreach ($this->postdata as $name => $content_file) |
---|
97 | { |
---|
98 | $data .= 'Content-Disposition: form-data; name="'.$name.'"; filename="'.$name.'"'.$lf; |
---|
99 | $data .= 'Content-Type: text/plain'.$lf.$lf; |
---|
100 | $data .= $content_file.$lf; |
---|
101 | $data .= '--'.$boundary.$lf; |
---|
102 | } |
---|
103 | |
---|
104 | $request_string .= 'Content-Length: '.strlen($data).$lf; |
---|
105 | $request_string .= 'Content-Type: multipart/form-data, boundary='.$boundary.$lf; |
---|
106 | } |
---|
107 | else |
---|
108 | { |
---|
109 | $data = ''; |
---|
110 | } |
---|
111 | $request_string .= $lf; |
---|
112 | |
---|
113 | fputs($fp, $request_string.$data); |
---|
114 | $this->sent = strlen($header)+strlen($data); |
---|
115 | |
---|
116 | $header = 1; |
---|
117 | socket_set_timeout($fp, 5); |
---|
118 | while ($line = fgets($fp, 4096)) |
---|
119 | { |
---|
120 | if ($header) |
---|
121 | { |
---|
122 | $http_header .= $line; |
---|
123 | } |
---|
124 | else |
---|
125 | { |
---|
126 | $file .= $line; |
---|
127 | } |
---|
128 | if ($header && $line == $lf) |
---|
129 | { |
---|
130 | $header = 0; |
---|
131 | } |
---|
132 | } |
---|
133 | fclose($fp); |
---|
134 | $this->header = $http_header; |
---|
135 | $this->content = $file; |
---|
136 | $this->recv = strlen($http_header)+strlen($file); |
---|
137 | $this->requested = true; |
---|
138 | } |
---|
139 | |
---|
140 | function url($url) |
---|
141 | { |
---|
142 | $this->url = parse_url($url); |
---|
143 | } |
---|
144 | |
---|
145 | // this is to send file-data to be accessed with $_FILES[$name] |
---|
146 | function set_postdata($name, $data) |
---|
147 | { |
---|
148 | $this->method = 'POST'; |
---|
149 | $this->postdata[$name] = $data; |
---|
150 | } |
---|
151 | |
---|
152 | // this function sends form data objects like $_POST[$name] = $data |
---|
153 | function set_postform($name, $data) |
---|
154 | { |
---|
155 | $this->method = 'POST'; |
---|
156 | $this->postform[$name] = $data; |
---|
157 | } |
---|
158 | |
---|
159 | function set_useragent($string) |
---|
160 | { |
---|
161 | $this->useragent = $string; |
---|
162 | } |
---|
163 | } |
---|
164 | ?> |
---|