简体   繁体   中英

My HTTP request works with cURL but with wp_remote_get() I get error 403 (Forbidden)

I am working on a WordPress plugin in which I must retrive product data from ShipHero's API.

When using cURL, I get a sucessful JSON. My code:

$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);

This is the response I get:

    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)

I am supposed to use the helper function wp_remote_get() instead. This is my code:

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

$response = wp_remote_get($url);

var_dump($response);

But what I get is a 403 message (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)

I have already tried to override the filter 'hhtp_headers_useragent' like in this question , and nothing changed.

I tried to do some Authorization, with the following code:

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

$response = wp_remote_request( $url, $args );

The username and password I used in this case were respectively the Shop Name and API Secret, given to me by ShipHero in their console (I do not know if that would be the case).

The response is still a 403 code, with the following message:

"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

Why does it work with cURL and not with wp_remote_get?

它与cURL一起使用,而不与wp_remote_get一起使用,因为它们没有为$ url使用完全相同的字符串,并且最后一个有错字。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM