$http_code, 'body' => $response, 'curl_error' => $curl_err ]; } // Process form submission $action = $_GET['action'] ?? ''; $result = null; $error = null; if ($action === 'profile' && !empty($_GET['company_number'])) { $companyNumber = urlencode(trim($_GET['company_number'])); $url = "$BASE_URL/company/$companyNumber/officers"; $resp = ch_get($url, $CH_API_KEY); if ($resp['curl_error']) { $error = 'cURL error: ' . htmlspecialchars($resp['curl_error']); } elseif ($resp['http_code'] >= 200 && $resp['http_code'] < 300) { $result = json_decode($resp['body'], true); } else { // Try to decode JSON error body if present $errBody = json_decode($resp['body'], true); $error = 'HTTP ' . $resp['http_code']; if ($errBody && isset($errBody['errors'])) { $error .= ': ' . json_encode($errBody['errors']); } elseif ($resp['body']) { $error .= ': ' . htmlspecialchars($resp['body']); } } } if ($action === 'search' && !empty($_GET['q'])) { // Example: search companies by name (search companies endpoint) $q = urlencode(trim($_GET['q'])); $url = "$BASE_URL/search/companies?q=$q"; // search companies endpoint $resp = ch_get($url, $CH_API_KEY); if ($resp['curl_error']) { $error = 'cURL error: ' . htmlspecialchars($resp['curl_error']); } elseif ($resp['http_code'] >= 200 && $resp['http_code'] < 300) { $result = json_decode($resp['body'], true); } else { $errBody = json_decode($resp['body'], true); $error = 'HTTP ' . $resp['http_code']; if ($errBody && isset($errBody['errors'])) { $error .= ': ' . json_encode($errBody['errors']); } elseif ($resp['body']) { $error .= ': ' . htmlspecialchars($resp['body']); } } } // ----------------------- // HTML UI // ----------------------- ?> Companies House API — PHP GET example

Companies House API — PHP GET example

Uses HTTP Basic Auth with your API key as the username and an empty password. (Don't commit your key.)

Get Company Profile (by company number)

Search Companies (by name/query)

Error

Result (pretty-printed JSON)

No JSON result returned.


Notes