'Opera/9.80 (X11; Linux i686; U; fr) Presto/2.2.15 Version/10.10', 'Accept-Language' => 'fr', ); static public $max_redirects = 2; static public $timeout = 5; static public function query($method='GET', $url, $cookies=array(), $content=array()) { $headers = self::$headers; $data = ''; if ($method == 'POST') { if (isset($content['json'])) { $data = json_encode($content['json']); $headers['Content-Length'] = strlen($data); $headers['Content-Type'] = 'application/json; charset=UTF-8'; } else { $data = http_build_query($content, null, '&'); $headers['Content-Length'] = strlen($data); $headers['Content-Type'] = 'application/x-www-form-urlencoded'; } } if (!empty($cookies)) { $headers['Cookie'] = ''; foreach ($cookies as $key=>$value) { if (!empty($headers['Cookie'])) $headers['Cookie'] .= '; '; $headers['Cookie'] .= $key . '=' . $value; } } $request = ''; foreach ($headers as $key=>$value) { $request .= $key . ': ' . $value . "\r\n"; } $context = stream_context_create( array( 'http' => array( 'method' => $method, 'timeout' => self::$timeout, 'header' => $request, 'content' => $data, 'max_redirects' => self::$max_redirects, ) ) ); unset($headers, $request, $content); $body = @file_get_contents($url, false, $context); if ($body === false) return false; $headers = array(); foreach ($http_response_header as $line) { if (preg_match('!^([a-z0-9_-]+): (.*)$!i', $line, $match)) { $key = $match[1]; if (strtolower($key) == 'set-cookie' && preg_match('!^([^=]+)=([^;]*)!', $match[2], $c_match)) { if (!array_key_exists('_cookies', $headers)) { $headers['_cookies'] = array(); } $headers['_cookies'][$c_match[1]] = urldecode($c_match[2]); } if (array_key_exists($key, $headers)) { if (!is_array($headers[$key])) { $headers[$key] = array($headers[$key]); } $headers[$key][] = $match[2]; } else { $headers[$key] = $match[2]; } } else { if (preg_match('!^HTTP/1\.[01] ([0-9]{3}) !', $line, $match)) { $headers['_code'] = $match[1]; } $headers[] = $line; } } return array($headers, $body); } } ?>