良好的邮递员请求在我的 php 网站上不起作用

问题描述 投票:0回答:1

在 Postman 上,我创建了一个查询,它返回结果,但生成的代码(PHP-CURL)对我来说似乎是正确的,但没有返回“false”:

方法POST
邮递员配置:

Params => nothing
Authorization => default (inherit auth …)
Headers => content-type: application/json, data-type: jsonp
Body => {
“Username”: “user-name”,
“Password”: “pass-word”
}

邮递员归还:

{
    "access_token": "xxxxxxx",
    "token_type": "xxx",
    "expires_in": 10799,
    "userName": "user-name",
    "issued": "2024-04-26T08:58:24Z",
    "expires": "2024-04-26T11:58:24Z",
    "error": "",
    "error_description": ""
}

邮递员代码生成:

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://XXX/api',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS =>'{
    "Username": "user-name",
    "Password": "pass-word"
}',
  CURLOPT_HTTPHEADER => array(
    'Content-Type: application/json',
    'dataType: jsonp'
  ),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;

但是$response = false

有关信息,我在本地服务器上工作

php postman
1个回答
0
投票

让我们解决这个问题:

验证连接和端点: 确保 URL“https://XXX/api”正确并且可以从本地服务器访问。

验证您的本地服务器环境是否可以访问互联网,因为它正在进行外部 API 调用。

检查错误: 将curl 执行包装在if 语句中以检查错误:

if ($response === false) {
    echo 'Curl error: ' . curl_error($curl);
} else {
    // Your existing code for the response
}

这将帮助您识别在curl执行过程中是否发生任何错误。

如果没有卷曲错误,请尝试检查响应以查看返回的内容:

var_dump($response);

检查服务器配置:

确保本地服务器的配置允许传出 HTTPS 请求。

查看 API 文档,确保您发送的请求格式正确并且没有其他要求或限制。

© www.soinside.com 2019 - 2024. All rights reserved.