PHP 微信一物一码接口对接 # 背景 公司要求对接微信一物一码功能进行二维码喷印。 微信文档: https://developers.weixin.qq.com/doc/offiaccount/Unique_Item_Code/Unique_Item_Code_API_Documentation.html # 上代码 这里token的获取使用了第三方库 `composer require overtrue/wechat` 附文档:https://packagist.org/packages/overtrue/wechat ```php ) * @create_time 2020-03-17 15:44:01 */ class Qrcode extends Controller { protected $app; protected $config; protected static $pass = "XXX";//微信会发到你的公众号邮箱 public function initialize() { $this->config = [ 'app_id' => 'XXX', 'secret' => 'XXX', 'response_type' => 'array', ]; $this->app = new Application($this->config); } /** * 下载一物一码 * @return success */ public function applycodedownload() { $app = $this->app; $application_id = 644504288; $accessToken = $app->access_token; $accessToken = $accessToken->getToken(); $accessToken = $accessToken['access_token']; $url = "https://api.weixin.qq.com/intp/marketcode/applycodedownload"; $client = new Client(); $query['access_token'] = $accessToken; $postData = [ 'application_id' => $application_id, 'code_start' => '0', 'code_end' => '9999' ]; $response = $client->request('POST', $url, ['query' => $query, 'json' => $postData]); $response = json_decode($response->getBody()->getContents(), true); $file_buffer = base64_decode($response['buffer']); $decyypt = openssl_decrypt($file_buffer, 'AES-128-CBC', self::$pass, OPENSSL_RAW_DATA, self::$pass); file_put_contents($application_id . ".txt", $decyypt); return "success"; } /** * 查询一物一码 * 预计等待10分钟 * @return {"errcode":0,"errmsg":"ok","application_id":644504288,"isv_application_id":"test00001","create_time":1584430326,"update_time":1584432000,"status":"FINISH","code_generate_list":[{"code_start":0,"code_end":9999}]} */ public function applycodequery() { $app = $this->app; $accessToken = $app->access_token; $accessToken = $accessToken->getToken(); $accessToken = $accessToken['access_token']; $url = "https://api.weixin.qq.com/intp/marketcode/applycodequery"; $client = new Client(); $query['access_token'] = $accessToken; $postData = [ 'application_id' => 644504288, 'isv_application_id' => 'test00001' ]; $response = $client->request('POST', $url, ['query' => $query, 'json' => $postData]); $response = json_decode($response->getBody()->getContents(), true); return $response; } /** * 申请一物一码 * @return {"errcode":0,"errmsg":"ok","application_id":644504288} */ public function applycode() { $app = $this->app; $accessToken = $app->access_token; $accessToken = $accessToken->getToken(); $accessToken = $accessToken['access_token']; $url = "https://api.weixin.qq.com/intp/marketcode/applycode"; $client = new Client(); $query['access_token'] = $accessToken; $postData = [ 'code_count' => 10000, 'isv_application_id' => 'test00001' ]; $response = $client->request('POST', $url, ['query' => $query, 'json' => $postData]); $response = json_decode($response->getBody()->getContents(), true); return $response; } } ``` # 附录 关于一物一码二维码加解密可以参看我的另一篇文章传送门