# Wordpress跨应用调取Magento产品数据

**URL:** https://forum.magentochina.org/t/wordpress%E8%B7%A8%E5%BA%94%E7%94%A8%E8%B0%83%E5%8F%96magento%E4%BA%A7%E5%93%81%E6%95%B0%E6%8D%AE/897
**Category:** Magento 2.X
**Tags:** api, magento2, golang
**Created:** [2021 年2 月 13 日 05:16 UTC](https://forum.magentochina.org/t/wordpress%E8%B7%A8%E5%BA%94%E7%94%A8%E8%B0%83%E5%8F%96magento%E4%BA%A7%E5%93%81%E6%95%B0%E6%8D%AE/897 "2021-02-13T05:16:04Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### 作者： ![Shuai](https://forum.magentochina.org/user_avatar/forum.magentochina.org/shuai/32/3_2.png) [@Shuai](https://forum.magentochina.org/u/Shuai)
#### 发布日期： [2021 年2 月 13 日 05:16 UTC](https://forum.magentochina.org/t/wordpress%E8%B7%A8%E5%BA%94%E7%94%A8%E8%B0%83%E5%8F%96magento%E4%BA%A7%E5%93%81%E6%95%B0%E6%8D%AE/897/1 "2021-02-13T05:16:04Z")

</div>

我这里有个需求就是Wordpress调取Magento的产品价格，并且Wordpress流量巨大(日均PV千万+)。  
这里我使用Golang作为一个单独的服务给Wordpress提供数据，使用的是之前介绍过的:  
[https://forum.magentochina.org/t/%E5%BC%80%E6%BA%90%E9%A1%B9%E7%9B%AE%E6%8E%A8%E8%8D%90magento2-api-golang/889/2](https://forum.magentochina.org/t/%E5%BC%80%E6%BA%90%E9%A1%B9%E7%9B%AE%E6%8E%A8%E8%8D%90magento2-api-golang/889/2)

Go端我使用gin作为web框架,并使用了PageCache抵御巨量访问请求，然后通过上面的API库使用reset API读取Magento2的数据。

话不多说，代码如下:

```golang

package main

import (
	"net/http"
	"github.com/gin-contrib/cache"
	"github.com/gin-contrib/cache/persistence"
	"github.com/gin-gonic/gin"
	"github.com/hermsi1337/go-magento2"
	"time"
)
// start gin framework

func setupRouter() *gin.Engine {
	//init gin 
	r := gin.Default()
	store := persistence.NewInMemoryStore(time.Second)
	r.GET("/sku/:sku", cache.CachePage(store, 5 * time.Minute, func(c *gin.Context) {
		SKU := c.Params.ByName("sku")
		if SKU != ""{
			respond := make(map[string]interface{})
			respond["Price"] = GetProductInfoBySKU(SKU)
			c.JSON(200, gin.H{"200": respond} )
			//log.Println(GetProductInfoBySKU(SKU))
		}else{
			c.JSON(http.StatusNotFound, gin.H{"message": "not found"})
		}
		
	}))
	return r
}

//GetProductInfoBySKU is Get product infomation By SKU.
func GetProductInfoBySKU(SKU string)(Price float64){
	// initiate storeconfig.
	storeConfig := &magento2.StoreConfig{
		Scheme: "https",
		HostName: "www.mywebsite.com",
		StoreCode: "default",
	}
	// initiate bearer payload
	bearerToken := "your magento token from adminplan"
	// create a new apiClient
	apiClient, err := magento2.NewAPIClientFromIntegration(storeConfig, bearerToken)
	if err != nil {
		panic(err)
	}
	// get product by SKU
		mProduct, err := magento2.GetProductBySKU(SKU, apiClient)
		if err != nil {
			panic(err)
		}
		Price = mProduct.Product.Price
	
//	log.Println(mProduct.Product.ProductLinks)
	return Price
}
func main() {
	r := setupRouter()
	r.Run("127.0.0.1:8080")
}

```

结果:

 ![image](https://forum.magentochina.org/uploads/default/original/1X/1d20ac262c72e5af568d485728e85dc7dfdff902.png)  
 ![image](https://forum.magentochina.org/uploads/default/original/1X/518edd1f628112f61f7652610d34d80d2cc8de75.png)  
初始化的时候速度大于2s，后续查找小于1s。在有cache的情况下，response速度为纳秒级。  
飞一般的速度。

Wordpress端不是本文重点，返回的Json自行处理。
