【发布时间】:2020-12-30 22:25:38
【问题描述】:
我正在为一个与第三方 HTTP API 交互的 wordpress 网站开发一个插件。
对于一个特定的请求,我完全依赖于响应标头。这是一个用于重定向的Location header,我需要从中获取url和查询参数。
请求:
return wp_remote_post($url, array(
'body' => http_build_query(array(
'token' => <omitted>,
'email' => <omitted>,
'password' => <omitted>
)),
'headers' => array(
'Content-Type' => 'application/x-www-form-urlencoded'
),
'redirection' => 0
));
在测试上面的请求时,我注意到headers 响应字段是null 和{}:
{
"headers": {},
"body": "",
"response": {
"code": 303,
"message": "See Other"
},
"cookies": [
{
"name": "JSESSIONID",
"value": "<omitted>",
"expires": null,
"path": "/<omitted>",
"domain": "test.<omitted>.com",
"host_only": true
}
],
"filename": null,
"http_response": {
"data": null,
"headers": null,
"status": null
}
}
请求在 Postman 中运行良好,所有预期的响应标头都清晰可见。我还关闭了 Postman 中的以下重定向,以便能够在重定向发生之前拦截 Location 标头。
我试过了:
- 另一个 wordpress 实例:同样的问题,没有标题
- 本地 docker wordpress 实例:同样的问题,没有标题
- 不同的 http 请求 (
GET https://example.com):同样的问题,没有标头 - 将重定向设置为
1而不是0:同样的问题,没有标头,但是重定向确实有效,这意味着存在Location标头并且wp_remote_post捡起了它 - 这让我更难理解为什么我看不到或检索到任何标题。
这是我 GET https://example.com 时的响应:
{
"headers": {},
"body": "<omitted>",
"response": {
"code": 200,
"message": "OK"
},
"cookies":[],
"filename": null,
"http_response": {
"data": null,
"headers": null,
"status": null
}
}
我已经搜索了好几个小时,但离解决这个问题还差一步。欢迎任何帮助。
编辑:有人向我询问 Postman 的 cURL PHP 代码:
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => '<omitted>/auth/login',
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 => 'token=<omitted>&email=<omitted>&password=<omitted>',
CURLOPT_HTTPHEADER => array(
'Content-Type: application/x-www-form-urlencoded',
'Cookie: JSESSIONID=<omitted>'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
【问题讨论】:
-
postman 生成的 PHP cURL 代码是什么样的?
-
@HowardE 在帖子末尾添加了 PHP cURL sn-p - 这是一个奇怪的 sn-p,因为它与实际的 Postman 设置不同,例如为生成的 cURL 启用了跟随重定向。
标签: wordpress http null header