我现在已经做了几次尝试,试图让这个荷兰的postcode.tech应用程序接口通过CURLOPT工作,但似乎没有什么能让它工作。
代码如下:
<?php
$headers = array(
'Bearer: f87d5d41-2495-47bf-8646-7fa1c46512e8'
);
echo $headers;
//The URL that we want to GET.
$url = 'https://postcode.tech/api/v1/postcode/full?postcode=1111XX&number=123';
//Initialize cURL.
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
//Set the URL that you want to GET by using the CURLOPT_URL option.
curl_setopt($ch, CURLOPT_URL, 'https://postcode.tech/api/v1/postcode/full?postcode=1446WJ&number=88');
//Set CURLOPT_RETURNTRANSFER so that the content is returned as a variable.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//Set CURLOPT_FOLLOWLOCATION to true to follow redirects.
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
//Execute the request.
$data = curl_exec($ch);
//Close the cURL handle.
curl_close($ch);
//Print the data out onto the page.
echo $data;
?>当我尝试的时候,我一直收到{ "message":"Unauthorized“},但是头/键是正确的。
发布于 2020-10-09 00:06:34
授权头的有效语法:
Authorization: <type> <credentials>
所以,改变:
$headers = array( 'Bearer: f87d5d41-2495-47bf-8646-7fa1c46512e8' );
至:
$headers = array("Authorization: Bearer f87d5d41-2495-47bf-8646-7fa1c46512e8");
输出:
{"postcode":"1446WJ","number":88,"street":"Volume","city":"Purmerend","municipality":"Purmerend","province":"Noord-Holland","geo":{"lat":52.509485977219,"lon":5.0036936976488}}有关授权头MDN web docs的一些背景知识
https://stackoverflow.com/questions/64265983
复制相似问题