我的 HTTP 请求适用于 cURL,但使用 wp_remote_get() 时出现错误 403(禁止)



我正在开发一个WordPress插件,我必须从ShipHero的API中检索产品数据。

使用 cURL 时,我得到了一个成功的 JSON。我的代码:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api-gateway.shiphero.com/v1/general-api/get-product/?token=$token&sku=$sku");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // Only for debugging locally
$response = curl_exec($ch);
curl_close($ch);
var_dump($response);

这是我得到的回应:

string '
{"Message": "success", "code": "200", "products": {"results": [{"sku": "999999999", "kit_components": [], "warehouses": [{"available": "0", "inventory_bin": "WA7", "inventory_overstock_bin": "", "backorder": "0", "warehouse": "Primary", "on_hand": "0", "allocated": "0"}], "build_kit": 0, "value": "0.00", "kit": 0}]}}
' (length=324)

我应该改用辅助函数 wp_remote_get((。这是我的代码:

$url = "https://api-gateway.shiphero.com/v1/general-api/
get-product/?token=$token&sku=$sku";
$response = wp_remote_get($url);
var_dump($response);

但我得到的是一条 403 消息(缺少身份验证令牌(:

array (size=6)
'headers' => 
object(Requests_Utility_CaseInsensitiveDictionary)[1416]
protected 'data' => 
array (size=8)
'content-type' => string 'application/json' (length=16)
'content-length' => string '42' (length=2)
'date' => string 'Tue, 22 Aug 2017 16:11:30 GMT' (length=29)
'x-amzn-requestid' => string '8f564453e0d-be543534d5-b554385ea7d' (length=36)
'x-amzn-errortype' => string 'MissingAuthenticationTokenException' (length=35)
'x-cache' => string 'Error from cloudfront' (length=21)
'via' => string '1.1 30f76efc52e6ca97f663d61e1f8e27ef.cloudfront.net (CloudFront)' (length=64)
'x-amz-cf-id' => string 'pmlZLt_c18F4wKiT7eSvfBHCcD-UwxPtP87dALZxIQ==' (length=56)
'body' => string '{"message":"Missing Authentication Token"}' (length=42)
'response' => 
array (size=2)
'code' => int 403
'message' => string 'Forbidden' (length=9)

我已经尝试像这个问题一样覆盖过滤器"hhtp_headers_useragent",但没有任何变化。

我尝试使用以下代码进行一些授权:

$args = array(
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( $username . ':' . $password ),
)
);
$response = wp_remote_request( $url, $args );

在这种情况下,我使用的用户名和密码分别是商店名称和API机密,由ShipHero在他们的控制台中提供给我(我不知道是否是这种情况(。

响应仍然是 403 代码,并显示以下消息:

"message":"Authorization header requires 'Credential' parameter. Authorization header requires 'Signature' parameter. Authorization header requires 'SignedHeaders' parameter. Authorization header requires existence of either a 'X-Amz-Date' or a 'Date' header

为什么它适用于 cURL 而不是wp_remote_get?

它适用于cURL,而不是wp_remote_get,因为它们没有使用完全相同的字符串进行$url,并且在最后一个中有一个拼写错误。

最新更新