PHP 接口:
以下接口为基础实现接口,可以自由调用。
以下基础方法实现了:
产品的自动发布,将产品直接发布到店铺中。
订单的自动检索,将店铺中需要履行的订单全部拉下来。
订单的自动履行(发货),将运单号同步到店铺后台发货。
产品上传推荐使用CSV 方式,即将多个产品按一定的格式汇出成一个CSV表格,将CSV上传到店铺产品管理中。
CSV参考:
参考:
https://shopify.dev/api/admin-rest/2021-10/resources/order#[get]/admin/api/2021-10/orders.json
参考:
https://shopify.dev/api/admin-rest/2021-10/resources/fulfillment#[post]/admin/api/2021-10/orders/{order_id}/fulfillments.json
shopify 是一家加拿大的自发货独立站,目前可以通过三种途径接入,
私有应用:仅对自己的店铺有效
公有应用:可以公开使用,对所有店铺授权
专属应用:需要自己创建到账号密码给到平台方授权
第一种适合于测试,第二种适合系统足够强大且完善的平台,第三种适合初创公司。
推荐使用第三种:
在店铺后台选择应用,右侧页面拉到最下面,点击管理专有应用
根据提示获取账号密码,创建类似于 如下链接的格式:
'https://' . $username . ':' . $password . '@' . $shop . '.myshopify.com/admin/api/2021-07/orders.json';
将产品的读取、写入,订单的读取、订单的履行等等权限开通(根据需要可以在店铺后台选择开通哪些权限)。
以下代码为基础实现接口,不包含实际业务接口。
<?php
/*** Created by PhpStorm.* User: Administrator* Date: 2021/5/19* Time: 10:52*/namespace app\shopify;class Shopify
{private $max_time;public function __construct(){$this->max_time = date('Y-m-d', strtotime('+3 days')) . 'T11:00:00-05:00';}/*** 检索位置列表* @param $username* @param $password* @param $shop* @param $u_id* @return mixed|string*/public function getThisLocations($username, $password, $shop, $u_id){$url = 'https://' . $username . ':' . $password . '@' . $shop . '.myshopify.com/admin/api/2021-07/locations.json';$result = $this->requestHandle($url, $data = [], $method = 'GET');if (isset($result['locations']) && is_array($result['locations'])) {foreach ($result['locations'] as $k => $node) {$data = ['location_id' => $node['id'],'u_id' => $u_id,'shop' => $shop,'name' => $node['name'],'address1' => $node['address1'],'address2' => $node['address2'],'city' => $node['city'],'zip' => $node['zip'],'province' => $node['province'],'country' => $node['country'],'phone' => $node['phone'],'country_code' => $node['country_code'],'country_name' => $node['country_name'],'province_code' => $node['province_code'],'active' => $node['active'],'localized_country_name' => $node['localized_country_name'],'localized_province_name' => $node['localized_province_name'],];if (db('shop_locations')->where('location_id', $node['id'])->where('u_id', $u_id)->value('id')) {db('shop_locations')->where('location_id', $node['id'])->where('u_id', $u_id)->update($data);} else {db('shop_locations')->insertGetId($data);}}}return $result;}/*** 获得订单信息* @param $username* @param $password* @param $shop* @param $time* @return mixed|string* https://shopify.dev/api/admin-rest/2021-10/resources/order#[get]/admin/api/2021-10/orders.json*/public function getShopifyOrderLists($username, $password, $shop, $time){if (!$time) {$time = $this->max_time;}$url = 'https://' . $username . ':' . $password . '@' . $shop . '.myshopify.com/admin/api/2021-07/orders.json';$data = ['financial_status' => 'paid','limit' => 50,'created_at_max' => $time];$result = $this->requestHandle($url, $data, $method = 'GET');return $result;}/*** 检索订单计数* @param $shop* @param $username* @param $password* @param $time* @return array|string*/public function getOrderCount($shop, $username, $password, $time){if (!$time) {$time = $this->max_time;}$url = 'https://' . $username . ':' . $password . '@' . $shop . '.myshopify.com/admin/api/2021-07/orders/count.json';$data = ['financial_status' => 'paid','created_at_max' => $time];$result = $this->requestHandle($url, $data, $method = 'GET');return $result;}/*** 获取店铺产品列表* @param array $opt* @return mixed*/public function getShopProductList($shop, $opt = []){$shop_stroe_list = db('shop_store')->where('url', $shop)->find();$username = $shop_stroe_list['username'];$password = $shop_stroe_list['password'];$data = ['status' => 'active'];$data = array_merge($data, $opt);