You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.php 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. use GuzzleHttp\Client;
  3. use GuzzleHttp\Exception\ClientException;
  4. use Phly\Http\ServerRequestFactory;
  5. use Phly\Http\ServerRequest;
  6. use Phly\Http\Response;
  7. require __DIR__.'/vendor/autoload.php';
  8. /**
  9. * Sends an HTTP response.
  10. *
  11. * @param Response $response
  12. */
  13. function sendResponse(Response $response)
  14. {
  15. http_response_code($response->getStatusCode());
  16. foreach ($response->getHeaders() as $header => $values) {
  17. if (strtolower($header) === 'content-type') {
  18. header(sprintf('%s: %s', $header, implode('; ', $values)));
  19. }
  20. }
  21. $body = $response->getBody();
  22. echo $body;
  23. return $body;
  24. }
  25. /**
  26. * Does a request to the API of Bittrex.
  27. *
  28. * @param ServerRequest $request
  29. * @param Client $client
  30. *
  31. * @return \GuzzleHttp\Psr7\Response
  32. */
  33. function doRequest(ServerRequest $request, Client $client, $host)
  34. {
  35. $headers = [];
  36. foreach ($request->getHeaders() as $name => $value) {
  37. $headers[$name] = $value[0];
  38. }
  39. $serverParams = $request->getServerParams();
  40. $path = !empty($serverParams['PATH_INFO']) ? $serverParams['PATH_INFO'] : $serverParams['REQUEST_URI'];
  41. $url = $host.$path;
  42. unset($headers['host']);
  43. return $client->request(
  44. $request->getMethod(),
  45. $url,
  46. [
  47. 'headers' => $headers,
  48. 'body' => $request->getBody(),
  49. ]
  50. );
  51. }
  52. try {
  53. $apiResponse = doRequest(
  54. ServerRequestFactory::fromGlobals(),
  55. new Client(),
  56. getenv('PHP_PROXY_REMOTEHOST')
  57. );
  58. } catch (ClientException $e) {
  59. $apiResponse = $e->getResponse();
  60. }
  61. return sendResponse(new Response(
  62. $apiResponse->getBody(),
  63. $apiResponse->getStatusCode(),
  64. $apiResponse->getHeaders()
  65. ));