Skip to content

Commit

Permalink
Merge pull request #97 from wikimedia/proxy-curl
Browse files Browse the repository at this point in the history
Use cURL instead of file_get_contents
  • Loading branch information
samwilson authored Nov 28, 2019
2 parents 787eaea + 21574f6 commit a58700c
Showing 1 changed file with 24 additions and 9 deletions.
33 changes: 24 additions & 9 deletions public_html/wikiWhoProxy.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php

header( 'Content-Type: application/json' );
header( 'Access-Control-Allow-Origin: *' );

// Restrict access to Wikipedias.
Expand All @@ -20,11 +19,27 @@
// Strip out /wikiwho for the Toolforge location.
$endpoint = preg_replace( '/^\/wikiwho/', '', $_SERVER['REQUEST_URI'] );
$redirectUrl = "https://api.wikiwho.net$endpoint";
$context = stream_context_create( [
'http' => [
'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;

0 comments on commit a58700c

Please sign in to comment.