2016-09-05 1 views
1

CURL PHP를 사용하여 Walmart Seller API를 사용하려고합니다. 어느 RSA PHP 라이브러리를 사용해야하는지 제안 할 수 있습니까? walmart를 호출하는 동안 인증 서명이 검증됩니다.월마트 판매자 API POST가 작동하지 않음, PHP에서 401 권한이 없음 만 제공

하나의 경험이 있으십니까?

$headers = array(
    'WM_SVC.NAME: Walmart Marketplace', 
    'WM_QOS.CORRELATION_ID: 14730688612', 
    'WM_SEC.TIMESTAMP:14730688612', 
    'WM_SEC.AUTH_SIGNATURE: XXXXXXXXXXX' 
    'WM_CONSUMER.ID: XXXXXXXXXXX', 
    'Content-Type: application/xml', 
    'Accept: application/xml', 
); 


$ch = curl_init(); 
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, 'GET'); 
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; 

Google 검색 중 찾을 수있는 참조를 사용했습니다.

- https://github.com/fillup/walmart-auth-signature-php 

답변

1

가장 힘든 부분은 서명을 생성하고 제대로 작동하도록하는 것입니다.

나는 여러 가지 지점에서이 조각들을 많이 가져다가 직선적 인 예를 제시 했으므로 잘 동작 할 것이다.

피드 상태를 모두 얻으려면 GET 호출을하고 싶다고합시다. 당신의 노력

// Your walmart info from walmart admin 
$walmart_consuer_id = XXXXXXXXXXXXX; 
$walmart_channel_type = XXXXXXXXXXXX; 

$request_type = "GET"; 

$url = "https://marketplace.walmartapis.com/v2/feeds"; 

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

$signature = getClientSignature($url, $request_type, $timestamp); 

$headers = array(); 
$headers[] = "Accept: application/xml"; 
$headers[] = "WM_SVC.NAME: Walmart Marketplace"; 
$headers[] = "WM_CONSUMER.ID: ".$walmart_consuer_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_FAILONERROR, true); 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $request_type); 
curl_setopt($ch, CURLOPT_AUTOREFERER, true); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 

$result = curl_exec($ch); 

function getClientSignature($url, $request_type, $timestamp) { 
    // Your walmart info from walmart admin 
    $walmart_secret = XXXXXXXXXXXXXXXX; 
    $walmart_consuer_id = XXXXXXXXXXXXX; 

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

    // Construct the data we want to sign 
    $data = $walmart_consuer_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); 
} 

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; 
} 
+0

감사합니다. 이미 해결되었습니다. – Zeolr

-1
$ch = curl_init(); 

$headers = $actual; 

$qos = uniqid(); 

$url='https://api-gateway.walmart.com/v3/items'; 

$options = 
array (
    CURLOPT_URL => $url, 

    CURLOPT_RETURNTRANSFER => true, 

    CURLOPT_TIMEOUT => 60, 

    CURLOPT_HEADER => false, 

    CURLOPT_POST => 1, 

    CURLOPT_HTTPHEADER => array(
    'WM_SVC.NAME: Drop Ship Vendor Services', 

    'WM_QOS.CORRELATION_ID:'.$qos, 

    'WM_SEC.TIMESTAMP:1451606400', 

    'WM_SEC.AUTH_SIGNATURE: '.$headers, 

    'WM_CONSUMER.ID:'.$WalmartConsumerID 
, 
    'WM_CONSUMER.CHANNEL.TYPE:**********', 

    'Accept: application/xml' 
     ), 

    CURLOPT_HTTPGET => true 
); 

curl_setopt_array($ch, $options); 

$response = curl_exec ($ch); 

print_r($response); 
+0

이 문제를 해결하는 방법을 알려주시겠습니까? –

0
<?php 
$consumer_id='xxxxxxxxxx'; 
$private_key='xxxxxxxxxx'; 
$timestamp='xxxxxxxx'; 
$correlation_id='xxxxxxxxxxx'; 
$channel_id='xxxxxxxxxx'; 

$endPoint = "v3/feeds?feedType=item"; 
$requestUrl = "https://marketplace.walmartapis.com/$endPoint"; 
$requestMethod = 'POST'; 
$signature = new Signature($consumer_id, $private_key, $requestUrl,  $requestMethod); 
$actual_signature = $signature->getSignature($timestamp); 

$file = '..../walmart_product.xml'; 

$headers = []; 
$headers[] = "WM_SVC.NAME: Walmart Marketplace"; 
$headers[] = "WM_QOS.CORRELATION_ID: ".$correlation_id; 
$headers[] = "WM_SEC.TIMESTAMP: ".$timestamp; 
$headers[] = "WM_SEC.AUTH_SIGNATURE: ".$actual_signature; 
$headers[] = "WM_CONSUMER.ID: " .$consumer_id; 
$headers[] = "Content-Type: multipart/form-data"; 
$headers[] = "Accept: application/xml"; 
$headers[] = "WM_CONSUMER.CHANNEL.TYPE: ".$channel_id; 
$headers[] = "HOST: marketplace.walmartapis.com"; 


$body ['file'] = new \CurlFile ($file, 'application/xml'); 

$ch = curl_init(); 
curl_setopt ($ch, CURLOPT_URL, $requestUrl); 
curl_setopt ($ch, CURLOPT_HTTPHEADER, $headers); 
curl_setopt ($ch, CURLOPT_HEADER, 1); 
curl_setopt ($ch, CURLOPT_POST, 1); 
curl_setopt ($ch, CURLOPT_POSTFIELDS, $body); 
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0); 
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true); 
$server_output = curl_exec ($ch); 
?> 
관련 문제