在下面的代码示例中,我将API端点替换为占位符端点,但php和node.js脚本中的占位符端点完全相同.还要注意的是,被击中的端点托管在Azure前门后面的Azure应用程序服务上.不过,我确实在应用程序服务日志(log)中看到了404个错误.

以下是php脚本:

$apiKey = "someAPIKey";
$accountId = "someAccountId";
$txRequest = createTransaction(); // This is JSON

$baseUri = "https://someapiendpoint.com/graphql";
$headers = [
    "Accept: application/json",
    "X-API-KEY: $apiKey",
    "Account: $accountId",
];

echo $txRequest;

$ch = curl_init($baseUri);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $txRequest);

$response = curl_exec($ch);
curl_close($ch);

if ($response === FALSE) {
    echo "cUrl error (#%d): %s<br>\n";
}

echo $response;

来自php函数的响应:

* Connected to someapiendpoint.com (xxx:xxx:xxx:xxx) port 443 (#0)
* ALPN: offers h2
* ALPN: offers http/1.1
* SSL connection using TLSv1.2 / ECDHE-RSA-AES128-GCM-SHA256
* ALPN: server accepted h2
* Server certificate:
*  subject: CN=someapiendpoint.com
*  start date: Jun 11 00:00:00 2023 GMT
*  expire date: Dec 11 23:59:59 2023 GMT
*  subjectAltName: host "someapiendpoint.com" matched cert's "someapiendpoint.com"
*  issuer: C=US; O=DigiCert, Inc.; CN=GeoTrust Global TLS RSA4096 SHA256 2022 CA1
*  SSL certificate verify result: self-signed certificate in certificate chain (19), continuing anyway.
* Using HTTP2, server supports multiplexing
* Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0
* h2h3 [:method: POST]
* h2h3 [:path: /graphql]
* h2h3 [:scheme: https]
* h2h3 [:authority: someapiendpoint.com]
* h2h3 [accept: application/json]
* h2h3 [x-api-key: someAPIKey]
* h2h3 [account: someAccountId]
* h2h3 [content-length: 1187]
* h2h3 [content-type: application/x-www-form-urlencoded]
* Using Stream ID: 1 (easy handle 0x264ace0f260)
> POST /graphql HTTP/2
Host: someapiendpoint.com
accept: application/json
x-api-key: someAPIKey
account: someAccountId
content-length: 1187
content-type: application/x-www-form-urlencoded

* Connection state changed (MAX_CONCURRENT_STREAMS == 128)!
* We are completely uploaded and fine
< HTTP/2 404 
< date: Thu, 05 Oct 2023 22:43:53 GMT
< content-length: 0
< x-robots-tag: noindex,nofollow
< x-correlation-id: 000000000000000000000000
< x-azure-ref: 0000000000000000000000000000000000000000
< x-cache: CONFIG_NOCACHE
<
* Connection #0 to host someapiendpoint.com left intact

下面是node.js脚本:

const response = await axios.post(
    'https://someapiendpoint.com/graphql',
    txRequest, // This is JSON
    {
      headers: {
        Accept: 'application/json',
        'X-API-KEY':
          'someAPIKey',
        Account: 'someAccountId',
      },
    }
  );
console.log(response.data);

Js脚本使用预期的响应正文响应200.

推荐答案

经过多次试错,我终于找到了问题所在.

我不得不添加标题"Content-Type"并将其设置为"application/json".一旦添加了这个标题,我得到了200的回复,一切都成功了.

添加的标题代码:

$headers = [
    "Accept: application/json",
    "Content-Type: application/json", // This is what I added
    "X-API-KEY: $apiKey",
    "Account: $accountId",
];

Php相关问答推荐

添加自定义Metabox到WooCommerce管理订单,启用HPOS

有条件的运费率基于购物车小计与WooPayments货币转换

PHP邮件表单无法识别重音/特殊字符

WooCommerce购物车中仅允许一个可下载产品

msgraph-sdk-php v2如何传递skiptoken到request?

如何将对我的域的请求重定向到子文件夹中的索引,同时保留域URL并使用.htaccess?

在没有谷歌云客户端PHP库的情况下将图像上传到PHP中的Google存储API?

基于不用于变体的产品属性隐藏多个WooCommerce发货方式

在WooCommerce中添加特定产品类型的百分比费用和固定费用

Laravel Carbon DiffForHumans选项(年,月,天)

限制联系人页面仅允许在WooCommerce中登录的用户

ANTLR4-lexer如何消耗更多的 token 并停止现有规则?

PHP Laravel 迁移文件不创建关系

如何更新 Laravel Eloquent 列强制转换为集合

将图像作为 PHP post 响应发送到浏览器

致命错误:未捕获 Google_Exception:客户端机密 JSON 文件无效.在 /somepath/lib/vendor/google/apiclient/src/Google/Client.php

在WooCommerce单产品页面中显示特价产品的节省金额

Laravel 8:会话不适用

带有 nginx 的 Symfony 不提供 .js 和 .css 文件

如何在光速网络服务器中使用 apache ..htaccess