I'm using cURL to get content of page from other site. When I open this page in my browser I have $_SERVER['SCRIPT_URL'] = '/content/'.
But when I get this page with cURL i have $_SERVER['SCRIPT_URL'] = '/content/index.php'.
What request should I send to have same SCRIPT_URL as in browser?
Here is my class to get page:
<?php
class CurlReader implements IReader
{
/**
* @var array
*/
protected $_lastResponseHeaders = null;
/**
* @param string $url
* @param array $context
* @return string
*/
public function read($url, array $context = null)
{
$this->_lastResponseHeaders = null;
$return = '';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADERFUNCTION, array(&$this, 'readHeader'));
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_ENCODING , 'gzip');
if (!empty($context['method'])) {
if (strtolower($context['method']) == 'post') {
curl_setopt($ch, CURLOPT_POST, true);
}
}
if (!empty($context['header'])) {
curl_setopt($ch, CURLOPT_HTTPHEADER, array_map('trim', explode("\r", $context['header'])));
}
$return = curl_exec($ch);
if(curl_exec($ch) === false) {
throw new ReaderException(curl_error($ch));
}
curl_close($ch);
return $return;
}
/**
* @return array
*/
public function getHeaders()
{
return $this->_lastResponseHeaders;
}
/**
* @param array $headers
*/
protected function readHeader($curl, $header)
{
if (strpos($header, 'HTTP') !== false) {
preg_match("/\d{3}/i", $header, $matches);
$this->_lastResponseHeaders['Status'] = (int) $matches[0];
} elseif (($pos = strpos($header, ':')) !== false) {
$name = trim(substr($header, 0, $pos));
$value = trim(substr($header, $pos + 1));
$this->_lastResponseHeaders[$name] = $value;
}
return strlen($header);
}
}