네이버 스마트스토어 API(커머스API 센터) PHP인증 참고할것
네이버 스마트스토어용 API에 대한것으로 스마트스토어가 있는 경우 가입하여 사용 할 수 있습니다.
아래는 되는지 확인하기 위해서 테스트로 만든것으로서 실제사용은 함수나 클래스로 간략하게 만들 필요가 있습니다.
필요한것들은 아래와 같습니다.
- CURL사용시 HTTP 헤더로 전달(인증등)
- CURL을 GET으로 사용
- CURL을 POST로 사용
- HTTP 헤더를 분리해서 배열에 담아 두었다가 필요한것 있는지 참고
<?php
const client_id = 'sssssssssssss';
const client_secret = '$1111111swssssssssssssssssss';
$timestamp = (int)(microtime(true) * 1000);
$password = client_id.'_'.$timestamp;
$hashed = crypt($password, client_secret);
$b64_hashed = base64_encode($hashed);
echo $b64_hashed . PHP_EOL;
# 1) 인증토큰
$aGetParm = [
"client_id" => client_id,
"timestamp" => $timestamp,
"grant_type" => "client_credentials",
"type" => "SELF",
"client_secret_sign" => "$b64_hashed"
];
$queryString = http_build_query($aGetParm);
echo $queryString . PHP_EOL;
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $queryString,
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo $queryString . PHP_EOL;
echo "cURL Error #:" . $err . PHP_EOL;
exit;
}
else {
echo '<pre>' . print_r($response, true) . '</pre>';
$oJson = json_decode($response);
$access_token = $oJson->access_token;
$expires_in = $oJson->expires_in;
$token_type = $oJson->token_type;
}
# 2) 주문조회 > 변경상품 주문내역 조회
// curl --request GET \
// --header 'Authorization: Bearer 인증토큰'
# &lastChangedTo=SOME_STRING_VALUE&lastChangedType=SOME_STRING_VALUE&moreSequence=SOME_STRING_VALUE&limitCount=SOME_INTEGER_VALUE
# Mon%20Apr%2011%202022%2015%3A21%3A44%20GMT%2B0900%20(%ED%95%9C%EA%B5%AD%20%ED%91%9C%EC%A4%80%EC%8B%9C)
# > Mon Apr 11 2022 15:21:44 GMT+0900 (한국 표준시)
$lastChangedFrom = '2023-01-01T01:01:01.000Z';
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.commerce.naver.com/external/v1/pay-order/seller/product-orders/last-changed-statuses?lastChangedFrom=$lastChangedFrom",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => [
"Authorization: Bearer $access_token"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err . PHP_EOL;
exit;
}
else {
echo $response . PHP_EOL;
$oJson = json_decode($response);
echo '<pre>' . print_r($oJson, true) . '</pre>';
# 3) 상품주문 상세내역 조회
// curl --request POST \
// --header 'Authorization: Bearer 인증토큰' \
// --header 'content-type: application/json' \
// --data '{"productOrderIds":["2022070112121201"]}'
// $oJson = json_decode($json_data);
// echo '<pre>' . print_r($oJson->data->lastChangeStatuses, true) . '</pre>';
// echo '<pre>' . print_r($oJson, true) . '</pre>';
$aProductID = array();
foreach ( $oJson->data->lastChangeStatuses as $aidx => $oList ) {
// echo '<pre>' . print_r($oList, true) . '</pre>';
/*
[orderId] => 2021110221054020
[productOrderId] => 2021110234567891
[productOrderStatus] => PURCHASE_DECIDED
[paymentDate] => 2021-11-01T11:12:12.0+09:00
[lastChangedDate] => 2022-01-01T12:12:12.0+09:00
[receiverAddressChanged] =>
[lastChangedType] => PURCHASE_DECIDED
*/
$productOrderId = $oList->productOrderId;
$aProductID[] = $productOrderId;
}
$aPostData = array();
if ( isset($aProductID) && count($aProductID) > 0 ) {
$aPostData['productOrderIds'] = $aProductID;
$json_data = json_encode($aPostData, JSON_UNESCAPED_UNICODE);
// echo $json_data . PHP_EOL;
# 상품주문 상세내역 조회
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $json_data,
CURLOPT_HTTPHEADER => [
"Authorization: Bearer $access_token",
"content-type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err . PHP_EOL;
} else {
echo $response . PHP_EOL;
}
}
}
헤더에서 얻을 정보는 아래 리미트 관련된 부분을 참고하세요.
카카오워크 링크를 확인 하시면 됩니다.
echo 'ratelimit-limit: ' . $aRtnInfo['ratelimit-limit'] . "\n";
echo 'ratelimit-remaining: ' . $aRtnInfo['ratelimit-remaining'] . "\n";
* HTTP 헤더 배열로 결과 얻기 참고할곳