Skip to content

Instantly share code, notes, and snippets.

@dvygolov
Created July 26, 2024 11:42

Revisions

  1. dvygolov created this gist Jul 26, 2024.
    48 changes: 48 additions & 0 deletions shakeshttps.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    <?php

    class ShakesRedirectCorrector
    {
    private function get_headers($url): array
    {
    $ch = curl_init();
    $headers = [];
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);

    curl_setopt($ch, CURLOPT_HEADERFUNCTION,
    function ($curl, $header) use (&$headers) {
    $len = strlen($header);
    $header = explode(':', $header, 2);
    if (count($header) < 2) // ignore invalid headers
    return $len;

    $headers[strtolower(trim($header[0]))][] = trim($header[1]);

    return $len;
    }
    );

    $data = curl_exec($ch);
    return $headers;
    }

    public function get_https_redirect(): string
    {
    $queries = array();
    parse_str($_SERVER['QUERY_STRING'], $queries);
    $url = "http://streamshakes.com/".$queries['q'];

    $prefix= "http://";
    $headers = $this->get_headers($url);
    $location = $headers['location'][0];
    if (strpos($location, $prefix) === 0)
    $location = "https://" . str_replace($prefix, "", $location);
    return $location;
    }
    }

    $r = new ShakesRedirectCorrector();
    $location = $r->get_https_redirect();
    header("Location: {$location}");
    ?>