Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

CarlEOgden's avatar

Ocp-Apim-Subscription-Key missing subscription key

Hi

Can anyone let me know what I've done wrong when trying to send this curl request. I keep getting:-

401 - Access denied due to missing subscription key. Make sure to include subscription key when making requests to an API

		// encode params to json
		$postData = json_encode($params);
		// start the request
		$header = array(
			'Authorization'				=> 'bearer '.$access_token,
			'Content-Type'				=> 'application/json',
			'Ocp-Apim-Subscription-Key'	=> '',
		);
		curl_setopt($ch, CURLOPT_URL, "https://..............azure-api.........................");
		curl_setopt($ch, CURLOPT_HTTP_VERSION, 'CURL_HTTP_VERSION_2');
		curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_HEADER, false);
		curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
		curl_setopt($ch, CURLOPT_POST, count($params));
		curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
		// $output contains the output string
		$output = curl_exec($ch);
		$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
		// close curl resource to free up system resources
		curl_close($ch);
		// store the returned results
		$results = json_decode($output);
		// if there is an statusCode, check for an error
		dd(get_defined_vars());

This morning I've added http_version_2 but I am yet to get this call working! I've obviously set something incorrect but cannot work out for the life of me what I've done wrong!

Cheers Carl.

0 likes
1 reply
CarlEOgden's avatar

Hi

After playing a bit more, if I change the $header variable to:-

$header = [];
$header[] = 'Content-Type: application/json';
$header[] = 'Ocp-Apim-Subscription-Key: $subscriptionKey;
$header[] = $access_type.': '.$access_token;

Now I don't get a 401 error, I get a 400 error - Malformed request

I now presume that it's connected but my body data is incorrect. I have asked our provider to find out if I am correct in this assumption!

Cheers Carl.

Please or to participate in this conversation.