123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- <?php
- namespace App\Lib\Weidian;
- class weidianApi
- {
- public static $access_token;
- const WEIDIAN_HOST = 'https://api.vdian.com';
- const WEIDIAN_TOKEN_URL = self::WEIDIAN_HOST."/token?grant_type=client_credential";
- public static function get_accesstoken($app_key, $secret)
- {
- $key = "token_" . $app_key;
- $token = \Cache::get($key);
- if(empty($token)) {
- $url = self::WEIDIAN_TOKEN_URL . "&appkey={$app_key}&secret={$secret}";
- $res = json_decode(self::ihttp_get($url), true);
- if($res['status']['status_code'] == 0) {
- $token = $res['result']['access_token'];
- \Cache::add($key,$token,intval($res['result']['expire_in'] )/60);
- }
- }
- self::$access_token = $token;
- return $token;
- }
- public static function get_categories($token )
- {
- $param = json_encode(['showNoCate' => 0]);
- $public = json_encode([
- 'method' => 'weidian.cate.get.list',
- 'access_token' => $token,
- 'version' => '1.0'
- ]);
- $url = self::WEIDIAN_HOST."/api?param={$param}&public={$public}";
- return json_decode(self::ihttp_get($url));
- }
- public static function create_goods($data, $token)
- {
- $param = json_encode($data);
- $public = json_encode([
- 'method' => 'vdian.item.add',
- 'access_token' => $token,
- 'version' => '1.0',
- 'format' => 'json',
- ]);
- $url = self::WEIDIAN_HOST."/api?param={$param}&public={$public}";
- // $url = addcslashes($url);
- var_dump($url);
- var_dump(file_get_contents($url));
- var_dump(self::ihttp_get($url));
- return json_decode(self::ihttp_get($url));
- }
- public static function ihttp_get($url)
- {
- $headers = ["Content-type: application/json;charset='utf-8'"];
- $ch=curl_init();
- curl_setopt($ch, CURLOPT_URL,$url );
- curl_setopt($ch, CURLOPT_HEADER, 0);
- curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
- $respon = curl_exec($ch);
- return $respon;
- }
- }
|