BIN lookup with PHP

This page uses PHP with the curl extension. Create an API key in the quickstart. Put the key in the environment variable BINLOOKUP_API_KEY.

Look up one BIN

<?php
$apiKey = getenv("BINLOOKUP_API_KEY");
$ch = curl_init("https://api.binlookupapi.com/v1/bin");
curl_setopt_array($ch, [
    CURLOPT_POST => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HEADER => true,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTPHEADER => [
        "Authorization: Bearer " . $apiKey,
        "Content-Type: application/json",
    ],
    CURLOPT_POSTFIELDS => json_encode(["number" => 42467101]),
]);

$raw = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
curl_close($ch);

$headers = substr($raw, 0, $headerSize);
$body = json_decode(substr($raw, $headerSize), true);

if ($status === 200) {
    echo $body["data"]["scheme"], "\n";
} elseif ($status === 401) {
    fwrite(STDERR, "The API key is missing or not valid.\n");
} elseif ($status === 403) {
    fwrite(STDERR, $body["error"] . ": " . $body["message"] . "\n");
} elseif ($status === 404) {
    fwrite(STDERR, "The BIN is not in the data.\n");
} elseif ($status === 429) {
    fwrite(STDERR, "The daily quota is used.\n");
} elseif ($status === 400) {
    fwrite(STDERR, $body["message"] . "\n");
} else {
    fwrite(STDERR, "The API returned HTTP {$status}.\n");
}

data contains the fields in the API reference. On BLAPI-150KPM and above, read $body["meta"]["longer_bin_available"].

The quota headers are in $headers. There is no limit on requests per second.

Look up many BINs

POST /v1/bins accepts 1 to 2,000 values. It requires BLAPI-500KPM, or a test-mode organisation.

<?php
$ch = curl_init("https://api.binlookupapi.com/v1/bins");
curl_setopt_array($ch, [
    CURLOPT_POST => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_TIMEOUT => 60,
    CURLOPT_HTTPHEADER => [
        "Authorization: Bearer " . getenv("BINLOOKUP_API_KEY"),
        "Content-Type: application/json",
    ],
    CURLOPT_POSTFIELDS => json_encode([
        "numbers" => [400062, "400597", 99999999],
    ]),
]);

$body = json_decode(curl_exec($ch), true);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

if ($status === 403 && ($body["error"] ?? "") === "FEATURE_NOT_AVAILABLE") {
    fwrite(STDERR, "Batch lookup needs the BLAPI-500KPM plan.\n");
    exit(1);
}

foreach ($body["results"] as $item) {
    if (isset($item["data"])) {
        echo $item["number"], " ", $item["data"]["scheme"], "\n";
    } else {
        echo $item["number"], " ", $item["error"], "\n";
    }
}

See Batch lookups. One item error does not stop the other items.