在PHP cURL中导出并运行Postman API时,调用不起作用

时间:2019-06-08 04:19:00

标签: php curl postman

我正在尝试创建一个API调用以获得访问令牌。我的电话正在邮递员那里工作。以下是提取的集合。

{
    "info": {
        "_postman_id": "1861078c-8a9a-49a2-8a4b-88591ca750a8",
        "name": "asdf",
        "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
    },
    "item": [
        {
            "name": "asdf Access Token Generation",
            "request": {
                "auth": {
                    "type": "basic",
                    "basic": [
                        {
                            "key": "password",
                            "value": "asd",
                            "type": "string"
                        },
                        {
                            "key": "username",
                            "value": "asd",
                            "type": "string"
                        }
                    ]
                },
                "method": "POST",
                "header": [
                    {
                        "key": "Content-Type",
                        "name": "Content-Type",
                        "value": "application/x-www-form-urlencoded",
                        "type": "text"
                    },
                    {
                        "warning": "This is a duplicate header and will be overridden by the Authorization header generated by Postman.",
                        "key": "Authorization",
                        "value": "Basic",
                        "type": "text"
                    },
                    {
                        "key": "grant_type",
                        "value": "client_credentials",
                        "type": "text"
                    }
                ],
                "body": {
                    "mode": "urlencoded",
                    "urlencoded": [
                        {
                            "key": "grant_type",
                            "value": "client_credentials",
                            "type": "text"
                        }
                    ]
                },
                "url": {
                    "raw": "https://uatopenapi.abc.com:8243/token",
                    "protocol": "https",
                    "host": [
                        "uatopenapi",
                        "abc",
                        "com"
                    ],
                    "port": "8243",
                    "path": [
                        "token"
                    ]
                }
            },
            "response": []
        }
    ]
}

我使用邮递员生成了以下代码。 (我实际上进行了许多调整,但由于基本代码一开始就没有作用,因此它们都没有用。为了方便理解我的问题,我展示了邮递员生成的代码。

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_PORT => "8243",
  CURLOPT_URL => "https://uatopenapi.abc.com:8243/token",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "grant_type=client_credentials",
  CURLOPT_HTTPHEADER => array(
    "Accept: */*",
    "Authorization: Basic YXNkOmFzZA==",
    "Cache-Control: no-cache",
    "Connection: keep-alive",
    "Content-Type: application/x-www-form-urlencoded",
    "Host: uatopenapi.abc.com:8243",
    "Postman-Token: 1e4bc145-76c9-40d4-bd80-c23af17fa8a4,b1c0e5ce-2142-40f3-a378-d536acd24d2e",
    "User-Agent: PostmanRuntime/7.13.0",
    "accept-encoding: gzip, deflate",
    "cache-control: no-cache",
    "content-length: 29"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

我从php脚本中收到的错误是 cURL错误#:无法连接到uatopenapi.abc.com端口8243:连接被拒绝

API要求我对凭据进行base64编码。在实时脚本中,我删除了所有邮递员令牌和其他元数据。我无法弄清楚为什么在导出并作为php脚本运行该邮递员集合时没有给出错误。

我尝试删除邮递员元数据值,删除端口号,用CURLOPT_POST => true替换请求类型并添加CURLOPT_SSL_VERIFYPEER => false

1 个答案:

答案 0 :(得分:1)

我建议您尝试删除从Postman上的代码生成器生成的所有content-length参数,postman-token,cookie参数,它应作为自己的特定请求工作。那是一个开始。还要检查是否需要某些.PEM证书才能进行SSL身份验证。

删除

"Postman-Token: 1e4bc145-76c9-40d4-bd80-c23af17fa8a4,b1c0e5ce-2142-40f3-a378-d536acd24d2e"
"content-length: 29"
"User-Agent: PostmanRuntime/7.13.0",