我使用cURL从其他网站获取网页内容。当我在浏览器中打开此页面时,我$_SERVER['SCRIPT_URL'] = '/content/'
。
但是当我用cURL获得此页面时,我有$_SERVER['SCRIPT_URL'] = '/content/index.php'
。
我应该发送哪些请求与浏览器中的SCRIPT_URL
相同?
这是我的课程:
<?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);
}
}
答案 0 :(得分:1)
尝试$_SERVER["SCRIPT_FILENAME"]
http://php.net/manual/en/reserved.variables.server.php:
&#39; SCRIPT_FILENAME&#39;
当前正在执行的脚本的绝对路径名。