MtG Card Data Collector

[ // 'user_agent' => '', // 'protocol_version' => 1.1 //]]); function get_card_data($url) { $fp = fopen($url, 'r'); $meta = stream_get_meta_data($fp); $content = stream_get_contents($fp); fclose($fp); $headers = []; foreach ($meta["wrapper_data"] as $line) { $parts = explode(":", $line, 2); if (isset($parts[1])) $headers[trim($parts[0])] = trim($parts[1]); } return ["meta" => $meta, "header" => $headers, "data" => json_decode($content, true)['cards'][0]]; } function x() { $engine = new StringTemplate\Engine; if (isset($_REQUEST['query'])) { $q = $_REQUEST['query']; if (strlen($q) < 3){ echo "

(屮゚Д゚)屮

"; return; } switch ($_REQUEST['format']) { case "set:num": $template = "{set}: {number}"; break; case "cardname": default: $template = "{name}"; } $lines = explode("\n", $q); if (count($lines) > 100) { echo "

Please request no more than 100 cards at a time

"; return; } echo '
';
        if (!empty($_REQUEST["prefix"]))
            echo '';

        $urls_only = false;
        foreach ($lines as $i => $line) {
            $parts = explode(' ', trim($line));
            $url = "https://api.magicthegathering.io/v1/cards?set=$parts[0]&number=$parts[1]";

            if (!$urls_only) {
                $response = get_card_data($url);
                if ($i > 1){
                #if (isset($response["header"]["Ratelimit-Remaining"]) and $response["header"]["Ratelimit-Remaining"] === "0") {
                    echo "
Rate limit exceeded. Please try again in a few hours or use the raw result URLs.

"; $urls_only = true; } } if (!empty($_REQUEST["prefix"])) echo "'; else echo "\n"; } if (!empty($_REQUEST["prefix"])) echo '
$parts[0] $parts[1]"; elseif ($urls_only) echo "$parts[0] $parts[1]: "; if (empty($response["data"])) echo "Card not found :("; elseif (!$urls_only) echo $engine->render($template, $response["data"]); else echo "$url"; if (!empty($_REQUEST["prefix"])) echo '
'; echo '

'; } } x() ?>

Please keep in mind the data source for this service enforces a daily request limit, so please don't waste it. If no cards are found and you are sure your set codes and card numbers are valid, try again in a few hours or use the raw result URLs.