0

I'm trying to send a request to Walmarts API but am getting a 'malformed' error when sending my GET request, hoping someone could help. Below is my code

class Admin2 {

    function __construct() {

    }
public function pkcs8_to_pem($der) {

      static $BEGIN_MARKER = "-----BEGIN PRIVATE KEY-----";
      static $END_MARKER = "-----END PRIVATE KEY-----";

      $value = base64_encode($der);

      $pem = $BEGIN_MARKER . "\n";
      $pem .= chunk_split($value, 64, "\n");
      $pem .= $END_MARKER . "\n";

      return $pem;
  }

  public function getClientSignature($url, $request_type, $timestamp) {
      // Your walmart info from walmart admin
      $walmart_secret = 'XXXXXXXXXXXXXXXXX';
      $walmart_consumer_id = 'XXXXXXXXXXXXXXXXX';

      // Get an openssl usable private key from the walmart supplied secret
      $pem = $this->pkcs8_to_pem(base64_decode($walmart_secret));
      $private_key = openssl_pkey_get_private($pem);

      // Construct the data we want to sign
      $data = $walmart_consumer_id."\n";
      $data .= $url."\n";
      $data .= $request_type."\n";
      $data .= $timestamp."\n";

      // Sign the data
      $hash = defined("OPENSSL_ALGO_SHA256") ? OPENSSL_ALGO_SHA256 : "sha256";
      if (!openssl_sign($data, $signature, $private_key, $hash)) {
        // ERROR
        return null;
      }

      return base64_encode($signature);
  }

  public function walmartAPI(){

      if (!extension_loaded('openssl')) {
            'no openssl extension loaded.';
        }

     // Your walmart info from walmart admin
    $walmart_consumer_id = 'XXXXXXXXXXXXXXXXX';
    $walmart_channel_type = 'XXXXXXXXXXXXXXXXX';

    $request_type = "GET";

    $url = "https://developer.walmart.com/proxy/item-api-doc-app/rest/v3/feeds?includeDetails=false&offset=0&limit=50";
    $url = urlencode ( $url );

    // We need a timestamp to generate the signature and to send as part of the header
    $timestamp = round(microtime(true) * 1000);

    $signature = $this->getClientSignature($url, $request_type, $timestamp);

    $headers = array();
    $headers[] = "Accept: application/xml";
    $headers[] = "WM_SVC.NAME: Walmart Marketplace";
    $headers[] = "WM_CONSUMER.ID: ".$walmart_consumer_id;
    $headers[] = "WM_SEC.TIMESTAMP: ".$timestamp;
    $headers[] = "WM_SEC.AUTH_SIGNATURE: ".$signature;
    $headers[] = "WM_QOS.CORRELATION_ID: ".mt_rand();
    $headers[] = "WM_CONSUMER.CHANNEL.TYPE: " .$walmart_channel_type;

    $ch = curl_init($url);

    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_URL,$requestUrl);
    curl_setopt($ch, CURLOPT_HEADER, TRUE);
    curl_setopt($ch, CURLOPT_VERBOSE, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $request_type);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $result = curl_exec( $ch );

    if(curl_errno($ch)):
        echo 'Curl error: '.curl_error($ch);
    endif;

    $xml = simplexml_load_string($result);

     print_r($xml);

  }

} // end class

$admin2 = new Admin2();

echo $admin2->walmartAPI();
0

Put your raw request/response data here so that i can help you. 1.Use this URI =>

https://marketplace.walmartapis.com/v3/feeds/{feedId}?includeDetails={includeDetails}&limit={limit}&offset={offset}

Instead :

https://developer.walmart.com/proxy/

2.$url = urlencode ( $url );

DO NOT URLENCODE , REMOVE IT.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.