如何使用PHP为Aliexpress API生成_aop_signature?

时间:2019-04-23 05:54:12

标签: php api aliexpress

我正在使用Aliexpress API generatePromotionLinks。该API需要一个_aop_signature

参数

URL是这个:

https://gw.api.alibaba.com/openapi/param2/1/portals.open/api.generatePromotionLinks/[appKey]?trackingId=[trackingId]&targetUrls=[url]&locale=[global]&linkType=HOT_PRODUCT_LINK&_aop_signature=[signature]

我想知道在哪里可以得到_aop_signature,或者如何使用PHP生成_aop_signature

1 个答案:

答案 0 :(得分:1)

<?php
 $url = 'https://gw.api.alibaba.com/openapi/';
 $appKey = ***;
 $appSecret ='***';
 $apiInfo = 'param2/1/portals.open/api.generatePromotionLinks/' . $appKey;
 $code_arr = array(
    'urls' => 'https://ru.aliexpress.com/af/mp3.html?d=y&origin=n&SearchText=mp3&catId=0&initiative_id=SB_20191106040548',
    'linkType' => 'SEARCH_LINK',
    'fields' => 'promotionUrls,trackingId,publisherId',
    'trackingId' => 'Please create a unique affiliate ID for your site(s).',

);
 $aliParams = array();
 foreach ($code_arr as $key => $val) {
     $aliParams[] = $key . $val;
 }
 sort($aliParams);
 $sign_str = join('', $aliParams);
 $sign_str = $apiInfo . $sign_str;
 $code_sign = strtoupper(bin2hex(hash_hmac("sha1", $sign_str, $appSecret, true)));

 $url = $url.$apiInfo.'?'.http_build_query($code_arr)."&_aop_signature=".$code_sign;
 $ch = curl_init();
 curl_setopt($ch, CURLOPT_HEADER, 0);
 curl_setopt($ch, CURLOPT_VERBOSE, 0);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
 curl_setopt($ch, CURLOPT_URL, $url);
 $response = curl_exec($ch);
 curl_close($ch);
 print_r($response);
?>