首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >由于某些原因,Postcode.tech授权无法工作

由于某些原因,Postcode.tech授权无法工作
EN

Stack Overflow用户
提问于 2020-10-08 23:38:53
回答 1查看 48关注 0票数 0

我现在已经做了几次尝试,试图让这个荷兰的postcode.tech应用程序接口通过CURLOPT工作,但似乎没有什么能让它工作。

代码如下:

代码语言:javascript
复制
    <?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“},但是头/键是正确的。

EN

回答 1

Stack Overflow用户

发布于 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");

输出:

代码语言:javascript
复制
{"postcode":"1446WJ","number":88,"street":"Volume","city":"Purmerend","municipality":"Purmerend","province":"Noord-Holland","geo":{"lat":52.509485977219,"lon":5.0036936976488}}

有关授权头MDN web docs的一些背景知识

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64265983

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档