I have developed a proxy service in php which uses php-curl to request another url on the same server. Some times it works great but other times it takes a really long time and a lot of the time it times out.
This application does not make use of sessions so it can't be a locked session issue. I tried session_write_close() as a test before calling curl_exec and it made no difference.
What is the cause of this inconsistent behavior? I expect it to respond immediately since the only work it is doing is serving up 302 redirects. I have pasted my proxy function below.
protected function proxy( $pURL, $opts = array() ){
$defaults = array(
'headers' => array(),
'follow' => true,
'return' => false,
'return_headers' => false,
'referer' => fp_get_config( 'referer_override' ),
'user_agent' => $_SERVER['HTTP_USER_AGENT'],
'timeout' => 30,
'connect_timeout' => 10,
'fresh_connect' => false
);
$options = array_merge( $defaults, $opts );
extract( $options );
$c = curl_init( $pURL );
curl_setopt_array( $c, array(
CURLOPT_HEADER => $return_headers,
CURLOPT_USERAGENT => $user_agent,
CURLOPT_REFERER => $referer,
CURLOPT_CONNECTTIMEOUT => $connect_timeout,
CURLOPT_TIMEOUT => $timeout,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => $follow,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_FRESH_CONNECT => $fresh_connect,
CURLOPT_AUTOREFERER => true
));
//session_write_close();
$response = curl_exec( $c );
if ( $response === false )
die("Proxy Error: " . curl_error( $c ) );
curl_close( $c );
if ( $return )
return $response;
else {
if ( $return_headers ){
list( $headerblock, $body ) = explode("\r\n\r\n", $response, 2);
$headers = explode("\r\n", $headerblock );
foreach( $headers as $header )
header( $header );
echo $body;
} else
echo $response;
}
exit;
}