php - curl - 在邮递员中测试的数据在 php 代码中不起作用
问题描述
我有一个端点和 API 密钥,我正在 Postman 中进行测试,我得到了数据响应。因此,Postman 中的设置正常工作,并且我的 PHP 代码中的 curl 响应在转储 curl 响应时返回 false,并且在触发 API 调用时返回代码 200。
我刚刚从我设置的邮递员标头中实现了一个端点和 API 密钥。我需要两个参数:int和customerNumber。在邮递员中,它们被设置为原始 JSON 数据。
有人能弄清楚我在做什么错吗?
public function call($action, $parameter)
{
$endpoint = 'https://api.myendpoint.co.uk/full-dinner-serve/la/la/land';
$apiKey = 'NNOsndosfl14FS45ddgdg1';
$ch = curl_init();
$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'x-api-key: ' . $apiKey;
curl_setopt($ch, CURLOPT_URL, $endpoint);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
$postBody = array (
'action' => $action,
'customerNumber' => $parameter
);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postBody));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$response = curl_exec($ch);
return json_decode($response, true);
}
然后调用它:
public function validate($customerNumber)
{
$response = $this->call(
'init',
$customerNumber
);
return $response;
}
在最终端点:
$response = $this->get('service')->validate(
$this->data['customer_number']
);
解决方案
推荐阅读
- amazon-web-services - lambda 函数如何在无服务器中工作?
- mysql - 如何将 Varchar 的长度限制在 2 到 100 之间
- seo - Google 搜索控制台无法获取站点地图 | “无法读取站点地图”
- c# - 如何在 UIStackView 内更改 UIButton 背景颜色
- android - 在颤动中填充列时出现屏幕溢出
- javascript - JavaScript - 测量远程页面的加载时间
- python - 使用 GPU 快速计算矩阵核的整数基
- java - 使用 Bouncy Castle (Java) 重建 ED25519 密钥
- c# - 如何通过 ButtonClick 事件启动动画?
- python - IOError: [Errno 24] 打开的文件太多 - 但我关闭了所有文件