我正在开发一个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消息(Missing Authentication Token):
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'就像在this question中一样,没有任何改变。
我尝试使用以下代码进行一些授权:
$args = array(
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( $username . ':' . $password ),
)
);
$response = wp_remote_request( $url, $args );
我在这种情况下使用的用户名和密码分别是ShipHero在他们的控制台中给我的商店名称和API密码(我不知道是否会出现这种情况)。
响应仍然是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?
答案 0 :(得分:0)
它适用于cURL而不是wp_remote_get,因为它们没有使用完全相同的$ url字符串,并且最后一个字符串中存在拼写错误。