From 21574f6e32567f69841e31ab8438908cec7a8dce Mon Sep 17 00:00:00 2001 From: MusikAnimal Date: Tue, 26 Nov 2019 14:17:55 -0500 Subject: [PATCH] Use cURL instead of file_get_contents This is because file_get_contents is slower and can't handle as big of responses. Also ensure our response code is the same as WikiWho's Bug: T231492 --- public_html/wikiWhoProxy.php | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/public_html/wikiWhoProxy.php b/public_html/wikiWhoProxy.php index 46f0aef..15caf0a 100644 --- a/public_html/wikiWhoProxy.php +++ b/public_html/wikiWhoProxy.php @@ -1,6 +1,5 @@ [ - 'method' => 'GET', - 'header' => 'Authorization: Basic ' . base64_encode( $cnf[ 'user' ] . ':' . $cnf[ 'password' ] ), - ], -] ); - -echo file_get_contents( $redirectUrl, false, $context ); + +// Setup cURL handler and make the request. +$ch = curl_init( $redirectUrl ); +curl_setopt( $ch, CURLOPT_HTTPHEADER, [ 'Content-Type: application/json' ] ); +curl_setopt( $ch, CURLOPT_USERPWD, $cnf[ 'user' ] . ':' . $cnf[ 'password' ] ); +curl_setopt( $ch, CURLOPT_TIMEOUT, 180); +curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true); +curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true ); +$content = curl_exec( $ch ); +$response = curl_getinfo( $ch ); + +// Throw exception if there was an error. +if ( $content === false ) { + http_response_code( 500 ); + throw new Exception( curl_error( $ch ), curl_errno( $ch ) ); +} + +curl_close( $ch ); + +// Ensure our response code and content type are the same as WikiWho's. +http_response_code( $response[ 'http_code' ] ); +header( 'Content-Type: ' . $response[ 'content_type' ] ); + +echo $content;