2 /**********************************************************************
3 Copyright (C) FrontAccounting, LLC.
4 Released under the terms of the GNU General Public License, GPL,
5 as published by the Free Software Foundation, either version 3
6 of the License, or (at your option) any later version.
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
14 Read content of remote url via http.
15 Does not require curl php extension nor allow_url_fopen=1.
17 function url_get_contents($url, $timeout=10)
19 // get the host name and url path
20 $parsedUrl = parse_url($url);
22 $host = $parsedUrl['host'];
24 if (isset($parsedUrl['path'])) {
25 $path = $parsedUrl['path'];
27 // the url is pointing to the host like http://www.mysite.com
31 if (isset($parsedUrl['query'])) {
32 $path .= '?' . $parsedUrl['query'];
35 if (isset($parsedUrl['port'])) {
36 $port = $parsedUrl['port'];
38 // most sites use port 80
44 // connect to the remote server
45 $fp = @fsockopen($host, $port, $errno, $errstr, $timeout );
49 // send the necessary headers to get the file
50 fputs($fp, "GET $path HTTP/1.0\r\n" .
52 (isset($parsedUrl['pass']) ? "Authorization: Basic ".
53 base64_encode($parsedUrl['user'].':'.$parsedUrl['pass'])."\r\n" : '').
54 "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.3) Gecko/20060426 Firefox/1.5.0.3\r\n" .
56 "Accept-Language: en-us,en;q=0.5\r\n" .
57 "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n" .
58 "Keep-Alive: 300\r\n" .
59 "Connection: keep-alive\r\n" .
60 "Referer: http://$host\r\n\r\n");
62 // retrieve the response from the remote server
64 while ($line = fread($fp, 4096)) {
66 if ($host=='localhost')
67 stream_set_blocking($fp, 0); // just after connection switch to nonblocking mode
71 if (!strpos($response, "200 OK\r\n"))
74 $pos = strpos($response, "\r\n\r\n");
76 $response = substr($response, $pos + 4);
79 // return the file content
83 function url_copy($from, $to, $timeout=10)
85 $f = fopen($to, 'wb');
86 fwrite($f, url_get_contents($from, $timeout));