文章列表


magento2中的api创建集成以及代码示例

<h5 style="color:red;">系统学习magento二次开发,推荐小册:<a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank">《Magento中文全栈二次开发 》</a></h5> <div class="image-container"> <p> <a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank"> <img src="https://www.maxiaoke.com/uploads/images/20230218/bb9c82995c24d1105676e02f373755f5.jpg" alt="Magento中文全栈二次开发"> </a> </p> </div> <div class="text-container" style="font-size:14px; color:#888"> <p>本小册面向Magento2以上版本,书代码及示例兼容magento2.0-2.4版本。涵盖了magento前端开发,后端开发,magento2主题,magento2重写,magento2 layout,magento2控制器,magento2 block等相关内容,带领您成为magento开发技术专家。</p> </div> <hr><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">在Magento 2中,可以通过API创建集成,这可以使第三方应用程序与Magento 2进行通信并访问Magento 2中的数据和功能。以下是如何在Magento 2中创建API集成的代码示例:</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">创建API用户</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">首先,您需要创建一个API用户,这将为第三方应用程序提供访问Magento 2的权限。在Magento 2的后台,依次单击“System”&gt;“All Users”,然后单击“Add New User”按钮。填写用户信息,例如用户名、电子邮件地址和密码,并选择API角色。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">创建API角色</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">接下来,您需要创建一个API角色,该角色将定义API用户的权限。在Magento 2的后台,依次单击“System”&gt;“User Roles”,然后单击“Add New Role”按钮。在“Role Information”选项卡中,输入角色名称,并在“Role Resources”选项卡中选择API资源。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">创建API集成</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">最后,您需要创建API集成,这将为第三方应用程序提供访问Magento 2的权限。在Magento 2的后台,依次单击“System”&gt;“Extensions”&gt;“Integrations”,然后单击“Add New Integration”按钮。在“Integration Info”选项卡中,输入名称,并选择API角色。在“API”选项卡中,选择您想要向第三方应用程序公开的Magento 2 API端点。完成后,单击“Save”按钮。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">使用API密钥</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">在保存API集成后,将生成一个API密钥。您需要将此密钥提供给第三方应用程序,以便它可以使用Magento 2 API访问Magento 2中的数据和功能。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">下面是一个简单的代码示例,演示如何使用Magento 2 API创建新的产品:</p><pre class="brush:as3;toolbar:false">&lt;?php //&nbsp;Magento&nbsp;2&nbsp;API配置 $base_url&nbsp;=&nbsp;&#39;https://your-magento2-store.com/rest&#39;; $api_user&nbsp;=&nbsp;&#39;your-api-username&#39;; $api_key&nbsp;=&nbsp;&#39;your-api-key&#39;; //&nbsp;新产品数据 $new_product&nbsp;=&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&#39;product&#39;&nbsp;=&gt;&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#39;sku&#39;&nbsp;=&gt;&nbsp;&#39;test-product&#39;, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#39;name&#39;&nbsp;=&gt;&nbsp;&#39;Test&nbsp;Product&#39;, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#39;price&#39;&nbsp;=&gt;&nbsp;9.99, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#39;status&#39;&nbsp;=&gt;&nbsp;1, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#39;visibility&#39;&nbsp;=&gt;&nbsp;4, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#39;type_id&#39;&nbsp;=&gt;&nbsp;&#39;simple&#39;, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#39;attribute_set_id&#39;&nbsp;=&gt;&nbsp;4, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#39;weight&#39;&nbsp;=&gt;&nbsp;1 &nbsp;&nbsp;&nbsp;&nbsp;] ]; //&nbsp;使用cURL发送POST请求 $ch&nbsp;=&nbsp;curl_init($base_url.&#39;/V1/products&#39;); curl_setopt($ch,&nbsp;CURLOPT_CUSTOMREQUEST,&nbsp;&#39;POST&#39;); curl_setopt($ch,&nbsp;CURLOPT_POSTFIELDS,&nbsp;json_encode($new_product)); curl_setopt($ch,&nbsp;CURLOPT_RETURNTRANSFER,&nbsp;true); curl_setopt($ch,&nbsp;CURLOPT_HTTPHEADER,&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&#39;Content-Type:&nbsp;application/json&#39;, &nbsp;&nbsp;&nbsp;&nbsp;&#39;Authorization:&nbsp;Bearer&nbsp;&#39;.$api_key ]); $result&nbsp;=&nbsp;curl_exec($ch); curl_close($ch); //&nbsp;输出结果 echo&nbsp;$result;</pre><p class="p3" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;PingFang SC&quot;; white-space: normal;">此示例使用<span class="s1" style="font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: normal; font-family: &quot;Helvetica Neue&quot;;">cURL</span>发送一个<span class="s1" style="font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: normal; font-family: &quot;Helvetica Neue&quot;;">POST</span>请求来创建新产品。在请求中,我们提供产品数据,并使用<span class="s1" style="font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: normal; font-family: &quot;Helvetica Neue&quot;;">Bearer</span>身份验证向<span class="s1" style="font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: normal; font-family: &quot;Helvetica Neue&quot;;">Magento 2 API</span>发送<span class="s1" style="font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: normal; font-family: &quot;Helvetica Neue&quot;;">API</span>密钥。完成后,我们输出<span class="s1" style="font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: normal; font-family: &quot;Helvetica Neue&quot;;">API</span>响应。</p><p><br/></p>

magento2中的api基于会话的身份验证

<h5 style="color:red;">系统学习magento二次开发,推荐小册:<a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank">《Magento中文全栈二次开发 》</a></h5> <div class="image-container"> <p> <a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank"> <img src="https://www.maxiaoke.com/uploads/images/20230218/bb9c82995c24d1105676e02f373755f5.jpg" alt="Magento中文全栈二次开发"> </a> </p> </div> <div class="text-container" style="font-size:14px; color:#888"> <p>本小册面向Magento2以上版本,书代码及示例兼容magento2.0-2.4版本。涵盖了magento前端开发,后端开发,magento2主题,magento2重写,magento2 layout,magento2控制器,magento2 block等相关内容,带领您成为magento开发技术专家。</p> </div> <hr><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">在Magento 2中,API基于会话的身份验证使您可以使用Magento 2的用户凭据进行API调用。这意味着您可以使用Magento 2中的用户帐户进行API操作,而无需创建专用的API帐户。以下是如何在Magento 2中进行基于会话的API身份验证的代码示例:</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">获取授权令牌</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">首先,您需要获取一个授权令牌,该令牌将用于后续API调用。在Magento 2的后台,依次单击“System”&gt;“All Users”,然后单击要用于API调用的用户。在“User Info”选项卡中,单击“User Token”按钮,然后单击“Create New Token”按钮。在弹出的对话框中,输入令牌名称,并单击“Save”按钮。完成后,将显示一个新的授权令牌。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">创建API客户端</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">接下来,您需要创建一个API客户端,该客户端将用于与Magento 2进行通信。在Magento 2的后台,依次单击“System”&gt;“Extensions”&gt;“Integrations”,然后单击“Add New Integration”按钮。在“Integration Info”选项卡中,输入名称,并单击“Save”按钮。在“API”选项卡中,选择您想要向API客户端公开的Magento 2 API端点。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">使用API密钥</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">在保存API客户端后,将生成一个API密钥。您需要将此密钥提供给API客户端,以便它可以使用Magento 2 API访问Magento 2中的数据和功能。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">进行API调用</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">现在,您可以使用授权令牌和API密钥进行基于会话的API身份验证。以下是一个简单的代码示例,演示如何使用Magento 2 API创建新的产品,并使用基于会话的身份验证:</p><pre class="brush:as3;toolbar:false"> &lt;?php //&nbsp;Magento&nbsp;2&nbsp;API配置 $base_url&nbsp;=&nbsp;&#39;https://your-magento2-store.com&#39;; $username&nbsp;=&nbsp;&#39;your-username&#39;; $password&nbsp;=&nbsp;&#39;your-password&#39;; //&nbsp;新产品数据 $new_product&nbsp;=&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&#39;product&#39;&nbsp;=&gt;&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#39;sku&#39;&nbsp;=&gt;&nbsp;&#39;test-product&#39;, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#39;name&#39;&nbsp;=&gt;&nbsp;&#39;Test&nbsp;Product&#39;, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#39;price&#39;&nbsp;=&gt;&nbsp;9.99, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#39;status&#39;&nbsp;=&gt;&nbsp;1, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#39;visibility&#39;&nbsp;=&gt;&nbsp;4, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#39;type_id&#39;&nbsp;=&gt;&nbsp;&#39;simple&#39;, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#39;attribute_set_id&#39;&nbsp;=&gt;&nbsp;4, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#39;weight&#39;&nbsp;=&gt;&nbsp;1 &nbsp;&nbsp;&nbsp;&nbsp;] ]; //&nbsp;获取授权令牌 $ch&nbsp;=&nbsp;curl_init($base_url.&#39;/rest/V1/integration/admin/token&#39;); curl_setopt($ch,&nbsp;CURLOPT_CUSTOMREQUEST,&nbsp;&#39;POST&#39;); curl_setopt($ch,&nbsp;CURLOPT_RETURNTRANSFER,&nbsp;true); curl_setopt($ch,&nbsp;CURLOPT_POSTFIELDS,&nbsp;json_encode([ &nbsp;&nbsp;&nbsp;&nbsp;&#39;username&#39;&nbsp;=&gt;&nbsp;$username, &nbsp;&nbsp;&nbsp;&nbsp;&#39;password&#39;&nbsp;=&gt;&nbsp;$password ])); curl_setopt($ch,&nbsp;CURLOPT_HTTPHEADER,&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&#39;Content-Type:&nbsp;application/json&#39;, &nbsp;&nbsp;&nbsp;&nbsp;&#39;Content-Length:&nbsp;&#39;&nbsp;.&nbsp;strlen(json_encode([ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#39;username&#39;&nbsp;=&gt;&nbsp;$username, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#39;password&#39;&nbsp;=&gt;&nbsp;$password &nbsp;&nbsp;&nbsp;&nbsp;])) ]); $token&nbsp;=&nbsp;curl_exec($ch); curl_close($ch); //&nbsp;使用cURL发送POST请求 $ch&nbsp;=&nbsp;curl_init($base_url.&#39;/rest/V1/products&#39;); curl_setopt($ch,&nbsp;CURLOPT_CUSTOMREQUEST,&nbsp;&#39;POST&#39;); curl_setopt($ch,&nbsp;CURLOPT_RETURNTRANSFER,&nbsp;true); curl_setopt($ch,&nbsp;CURLOPT_POSTFIELDS,&nbsp;json_encode($new_product)); curl_setopt($ch,&nbsp;CURLOPT_HTTPHEADER,&nbsp;[ &#39;Content-Type:&nbsp;application/json&#39;, &#39;Authorization:&nbsp;Bearer&nbsp;&#39;&nbsp;.&nbsp;$token ]); $response&nbsp;=&nbsp;curl_exec($ch); curl_close($ch); echo&nbsp;$response; ?&gt;</pre><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">在此示例中,我们首先使用curl发送POST请求以获取授权令牌。然后,我们使用cURL发送另一个POST请求来创建新产品,其中包括授权令牌作为HTTP头。最后,我们打印API的响应。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">请注意,此代码示例仅用于说明如何使用Magento 2 API进行基于会话的身份验证。实际使用中,您需要考虑更多的错误处理和安全性措施,例如SSL证书验证。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p><br/></p>

magento2中的api基于 OAuth 的身份验证

<h5 style="color:red;">系统学习magento二次开发,推荐小册:<a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank">《Magento中文全栈二次开发 》</a></h5> <div class="image-container"> <p> <a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank"> <img src="https://www.maxiaoke.com/uploads/images/20230218/bb9c82995c24d1105676e02f373755f5.jpg" alt="Magento中文全栈二次开发"> </a> </p> </div> <div class="text-container" style="font-size:14px; color:#888"> <p>本小册面向Magento2以上版本,书代码及示例兼容magento2.0-2.4版本。涵盖了magento前端开发,后端开发,magento2主题,magento2重写,magento2 layout,magento2控制器,magento2 block等相关内容,带领您成为magento开发技术专家。</p> </div> <hr><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">Magento 2 中的 API 基于 OAuth 的身份验证,下面是一些代码示例。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">首先,您需要在 Magento 后台创建一个新的 OAuth 客户端,可以在 &quot;系统&quot; -&gt; &quot;集成&quot; -&gt; &quot;OAuth 客户端&quot; 中完成。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">然后,您可以使用以下代码示例来获取访问令牌并调用 API:</p><pre class="brush:as3;toolbar:false">//&nbsp;设置&nbsp;API&nbsp;地址 $url&nbsp;=&nbsp;&quot;https://example.com/rest/V1/customers/1&quot;; //&nbsp;设置&nbsp;OAuth&nbsp;认证信息 $consumerKey&nbsp;=&nbsp;&#39;your_consumer_key&#39;; $consumerSecret&nbsp;=&nbsp;&#39;your_consumer_secret&#39;; $accessToken&nbsp;=&nbsp;&#39;your_access_token&#39;; $accessTokenSecret&nbsp;=&nbsp;&#39;your_access_token_secret&#39;; //&nbsp;创建&nbsp;OAuth&nbsp;客户端 $oauthClient&nbsp;=&nbsp;new&nbsp;\OAuth($consumerKey,&nbsp;$consumerSecret,&nbsp;OAUTH_SIG_METHOD_HMACSHA1,&nbsp;OAUTH_AUTH_TYPE_AUTHORIZATION); $oauthClient-&gt;setToken($accessToken,&nbsp;$accessTokenSecret); //&nbsp;发送&nbsp;API&nbsp;请求 $oauthClient-&gt;fetch($url); //&nbsp;获取&nbsp;API&nbsp;响应 $response&nbsp;=&nbsp;$oauthClient-&gt;getLastResponse(); //&nbsp;解码&nbsp;JSON&nbsp;响应 $data&nbsp;=&nbsp;json_decode($response); //&nbsp;处理响应数据 //&nbsp;...</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">在上面的示例中,$url 是要调用的 API 的 URL。$consumerKey、$consumerSecret、$accessToken 和 $accessTokenSecret 是在 Magento 后台创建的 OAuth 客户端的信息。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">然后,使用 OAuth 客户端创建一个请求并调用 API。最后,您可以使用 json_decode() 函数解码响应数据并对其进行处理。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p3" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;PingFang SC&quot;; white-space: normal;">注意:在实际应用中,您可能需要处理<span class="s1" style="font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: normal; font-family: &quot;Helvetica Neue&quot;;"> API </span>响应中的错误,例如身份验证错误或请求参数错误。</p><p><br/></p>

magento2中的api基于令牌的身份验证

<h5 style="color:red;">系统学习magento二次开发,推荐小册:<a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank">《Magento中文全栈二次开发 》</a></h5> <div class="image-container"> <p> <a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank"> <img src="https://www.maxiaoke.com/uploads/images/20230218/bb9c82995c24d1105676e02f373755f5.jpg" alt="Magento中文全栈二次开发"> </a> </p> </div> <div class="text-container" style="font-size:14px; color:#888"> <p>本小册面向Magento2以上版本,书代码及示例兼容magento2.0-2.4版本。涵盖了magento前端开发,后端开发,magento2主题,magento2重写,magento2 layout,magento2控制器,magento2 block等相关内容,带领您成为magento开发技术专家。</p> </div> <hr><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">Magento 2 中的 API 还支持基于令牌的身份验证。下面是一些代码示例。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">首先,您需要在 Magento 后台创建一个新的 API 访问令牌,可以在 &quot;系统&quot; -&gt; &quot;集成&quot; -&gt; &quot;API 访问令牌&quot; 中完成。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">然后,您可以使用以下代码示例来调用 API:</p><pre class="brush:as3;toolbar:false">//&nbsp;设置&nbsp;API&nbsp;地址 $url&nbsp;=&nbsp;&quot;https://example.com/rest/V1/customers/1&quot;; //&nbsp;设置访问令牌 $accessToken&nbsp;=&nbsp;&#39;your_access_token&#39;; //&nbsp;创建请求头 $headers&nbsp;=&nbsp;array( &nbsp;&nbsp;&nbsp;&nbsp;&#39;Authorization:&nbsp;Bearer&nbsp;&#39;&nbsp;.&nbsp;$accessToken, &nbsp;&nbsp;&nbsp;&nbsp;&#39;Content-Type:&nbsp;application/json&#39; ); //&nbsp;发送&nbsp;API&nbsp;请求 $ch&nbsp;=&nbsp;curl_init($url); curl_setopt($ch,&nbsp;CURLOPT_HTTPHEADER,&nbsp;$headers); curl_setopt($ch,&nbsp;CURLOPT_RETURNTRANSFER,&nbsp;true); $response&nbsp;=&nbsp;curl_exec($ch); curl_close($ch); //&nbsp;解码&nbsp;JSON&nbsp;响应 $data&nbsp;=&nbsp;json_decode($response); //&nbsp;处理响应数据 //&nbsp;...</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">在上面的示例中,$url 是要调用的 API 的 URL。$accessToken 是在 Magento 后台创建的 API 访问令牌。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">然后,您可以创建一个包含访问令牌的请求头,并使用 cURL 函数发送 API 请求。最后,您可以使用 json_decode() 函数解码响应数据并对其进行处理。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p3" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;PingFang SC&quot;; white-space: normal;">注意:在实际应用中,您可能需要处理<span class="s1" style="font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: normal; font-family: &quot;Helvetica Neue&quot;;"> API </span>响应中的错误,例如身份验证错误或请求参数错误。</p><p><br/></p>

magento2中的api验证以及代码示例

<h5 style="color:red;">系统学习magento二次开发,推荐小册:<a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank">《Magento中文全栈二次开发 》</a></h5> <div class="image-container"> <p> <a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank"> <img src="https://www.maxiaoke.com/uploads/images/20230218/bb9c82995c24d1105676e02f373755f5.jpg" alt="Magento中文全栈二次开发"> </a> </p> </div> <div class="text-container" style="font-size:14px; color:#888"> <p>本小册面向Magento2以上版本,书代码及示例兼容magento2.0-2.4版本。涵盖了magento前端开发,后端开发,magento2主题,magento2重写,magento2 layout,magento2控制器,magento2 block等相关内容,带领您成为magento开发技术专家。</p> </div> <hr><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">Magento 2提供了多个API,可以通过REST或SOAP协议进行访问。API的访问需要验证,这可以通过OAuth或Token-Based验证方法完成。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">OAuth验证</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">OAuth是一种常见的身份验证和授权机制。在OAuth身份验证期间,用户向应用程序提供他们的凭据,并授权应用程序访问他们的数据。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">在Magento 2中,使用OAuth验证需要以下步骤:</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">创建一个OAuth客户端。可以在Magento后台中的“系统”&gt;“集成”&gt;“OAuth客户端”下创建。</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">获取访问令牌。可以使用OAuth的授权码或密码授权模式来获取访问令牌。</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">使用访问令牌访问API。</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">以下是使用OAuth访问Magento 2 API的示例代码:</p><pre class="brush:as3;toolbar:false"> //设置OAuth访问令牌 $token&nbsp;=&nbsp;&#39;your_access_token&#39;; //设置API的URL $url&nbsp;=&nbsp;&#39;https://example.com/rest/V1/customers/1&#39;; //创建HTTP客户端 $client&nbsp;=&nbsp;new&nbsp;\Zend_Http_Client($url); //设置请求头 $client-&gt;setHeaders([ &nbsp;&nbsp;&nbsp;&nbsp;&#39;Authorization&#39;&nbsp;=&gt;&nbsp;&#39;Bearer&nbsp;&#39;&nbsp;.&nbsp;$token, &nbsp;&nbsp;&nbsp;&nbsp;&#39;Content-Type&#39;&nbsp;=&gt;&nbsp;&#39;application/json&#39;, ]); //发送GET请求 $response&nbsp;=&nbsp;$client-&gt;request(&#39;GET&#39;); //打印响应结果 echo&nbsp;$response-&gt;getBody();</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">Token-Based验证</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">Token-Based身份验证是另一种常见的身份验证方法。在此身份验证期间,用户提供其凭据,应用程序会创建一个唯一的令牌,并将其发送给用户。用户可以使用此令牌访问API。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">在Magento 2中,使用Token-Based身份验证需要以下步骤:</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">创建一个API访问令牌。可以在Magento后台中的“系统”&gt;“其他设置”&gt;“Magento API”下创建。</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">使用访问令牌访问API。</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">以下是使用Token-Based访问Magento 2 API的示例代码:</p><pre class="brush:as3;toolbar:false"> //设置API访问令牌 $token&nbsp;=&nbsp;&#39;your_access_token&#39;; //设置API的URL $url&nbsp;=&nbsp;&#39;https://example.com/rest/V1/customers/1&#39;; //创建HTTP客户端 $client&nbsp;=&nbsp;new&nbsp;\Zend_Http_Client($url); //设置请求头 $client-&gt;setHeaders([ &nbsp;&nbsp;&nbsp;&nbsp;&#39;Authorization&#39;&nbsp;=&gt;&nbsp;&#39;Bearer&nbsp;&#39;&nbsp;.&nbsp;$token, &nbsp;&nbsp;&nbsp;&nbsp;&#39;Content-Type&#39;&nbsp;=&gt;&nbsp;&#39;application/json&#39;, ]); //发送GET请求 $response&nbsp;=&nbsp;$client-&gt;request(&#39;GET&#39;); //打印响应结果 echo&nbsp;$response-&gt;getBody();</pre><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p>

magento2中的api使用 SOAP 服务

<h5 style="color:red;">系统学习magento二次开发,推荐小册:<a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank">《Magento中文全栈二次开发 》</a></h5> <div class="image-container"> <p> <a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank"> <img src="https://www.maxiaoke.com/uploads/images/20230218/bb9c82995c24d1105676e02f373755f5.jpg" alt="Magento中文全栈二次开发"> </a> </p> </div> <div class="text-container" style="font-size:14px; color:#888"> <p>本小册面向Magento2以上版本,书代码及示例兼容magento2.0-2.4版本。涵盖了magento前端开发,后端开发,magento2主题,magento2重写,magento2 layout,magento2控制器,magento2 block等相关内容,带领您成为magento开发技术专家。</p> </div> <hr><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">Magento 2提供了SOAP API,可以使用SOAP协议访问Magento 2 API。SOAP API提供了许多服务,可以访问Magento 2中的各种数据,如产品、类别、客户和订单等。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">以下是使用SOAP API访问Magento 2 API的示例代码:</p><pre class="brush:as3;toolbar:false">//&nbsp;设置Magento&nbsp;2&nbsp;API的基本URL和访问令牌 $baseUrl&nbsp;=&nbsp;&#39;https://example.com&#39;; $accessToken&nbsp;=&nbsp;&#39;your_access_token&#39;; //&nbsp;创建SOAP客户端 $options&nbsp;=&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&#39;soap_version&#39;&nbsp;=&gt;&nbsp;SOAP_1_2, &nbsp;&nbsp;&nbsp;&nbsp;&#39;stream_context&#39;&nbsp;=&gt;&nbsp;stream_context_create([ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#39;http&#39;&nbsp;=&gt;&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#39;header&#39;&nbsp;=&gt;&nbsp;&#39;Authorization:&nbsp;Bearer&nbsp;&#39;&nbsp;.&nbsp;$accessToken &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;] &nbsp;&nbsp;&nbsp;&nbsp;]) ]; $client&nbsp;=&nbsp;new&nbsp;\SoapClient($baseUrl&nbsp;.&nbsp;&#39;/soap/default?wsdl&amp;services=salesOrderRepositoryV1&#39;,&nbsp;$options); //&nbsp;创建SOAP请求参数 $params&nbsp;=&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&#39;searchCriteria&#39;&nbsp;=&gt;&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#39;pageSize&#39;&nbsp;=&gt;&nbsp;10, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#39;currentPage&#39;&nbsp;=&gt;&nbsp;1, &nbsp;&nbsp;&nbsp;&nbsp;] ]; //&nbsp;发送SOAP请求 $result&nbsp;=&nbsp;$client-&gt;salesOrderRepositoryV1GetList($params); //&nbsp;打印响应结果 print_r($result);</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">在此示例中,我们使用SoapClient类创建了一个SOAP客户端。然后,我们设置了请求头,其中包含访问令牌。接下来,我们使用salesOrderRepositoryV1服务和salesOrderRepositoryV1GetList方法发送了一个SOAP请求,以获取前10个订单。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p3" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;PingFang SC&quot;; white-space: normal;">请注意,这只是一个简单的示例。在实际使用中,您需要根据自己的需求和<span class="s1" style="font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: normal; font-family: &quot;Helvetica Neue&quot;;">API</span>文档来创建<span class="s1" style="font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: normal; font-family: &quot;Helvetica Neue&quot;;">SOAP</span>请求参数和解析响应结果。</p><p><br/></p>

magento2中的api使用 cURL 运行请求以及代码示例

<h5 style="color:red;">系统学习magento二次开发,推荐小册:<a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank">《Magento中文全栈二次开发 》</a></h5> <div class="image-container"> <p> <a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank"> <img src="https://www.maxiaoke.com/uploads/images/20230218/bb9c82995c24d1105676e02f373755f5.jpg" alt="Magento中文全栈二次开发"> </a> </p> </div> <div class="text-container" style="font-size:14px; color:#888"> <p>本小册面向Magento2以上版本,书代码及示例兼容magento2.0-2.4版本。涵盖了magento前端开发,后端开发,magento2主题,magento2重写,magento2 layout,magento2控制器,magento2 block等相关内容,带领您成为magento开发技术专家。</p> </div> <hr><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">Magento 2 API也可以使用cURL库进行访问,以下是使用cURL访问Magento 2 API的示例代码:</p><pre class="brush:as3;toolbar:false">//&nbsp;设置Magento&nbsp;2&nbsp;API的基本URL和访问令牌 $baseUrl&nbsp;=&nbsp;&#39;https://example.com&#39;; $accessToken&nbsp;=&nbsp;&#39;your_access_token&#39;; //&nbsp;创建cURL资源 $ch&nbsp;=&nbsp;curl_init(); //&nbsp;设置请求URL和请求头 curl_setopt($ch,&nbsp;CURLOPT_URL,&nbsp;$baseUrl&nbsp;.&nbsp;&#39;/rest/V1/customers/1&#39;); curl_setopt($ch,&nbsp;CURLOPT_HTTPHEADER,&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&#39;Authorization:&nbsp;Bearer&nbsp;&#39;&nbsp;.&nbsp;$accessToken, &nbsp;&nbsp;&nbsp;&nbsp;&#39;Content-Type:&nbsp;application/json&#39; ]); //&nbsp;执行请求并获取响应结果 curl_setopt($ch,&nbsp;CURLOPT_RETURNTRANSFER,&nbsp;true); $response&nbsp;=&nbsp;curl_exec($ch); //&nbsp;检查请求是否成功 if&nbsp;($response&nbsp;===&nbsp;false)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;&#39;Error:&nbsp;&#39;&nbsp;.&nbsp;curl_error($ch); }&nbsp;else&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;打印响应结果 &nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;$response; } //&nbsp;关闭cURL资源 curl_close($ch);</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">在此示例中,我们使用curl_init()函数创建了一个cURL资源。然后,我们设置了请求URL和请求头,并将CURLOPT_RETURNTRANSFER选项设置为true,以便获取响应结果。最后,我们使用curl_exec()函数执行请求,并检查是否有错误发生。如果请求成功,我们将打印响应结果。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p3" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;PingFang SC&quot;; white-space: normal;">请注意,这只是一个简单的示例。在实际使用中,您需要根据自己的需求和<span class="s1" style="font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: normal; font-family: &quot;Helvetica Neue&quot;;">API</span>文档来设置请求参数和解析响应结果。</p><p><br/></p>

magento2中的api构造一个请求以及代码示例

<h5 style="color:red;">系统学习magento二次开发,推荐小册:<a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank">《Magento中文全栈二次开发 》</a></h5> <div class="image-container"> <p> <a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank"> <img src="https://www.maxiaoke.com/uploads/images/20230218/bb9c82995c24d1105676e02f373755f5.jpg" alt="Magento中文全栈二次开发"> </a> </p> </div> <div class="text-container" style="font-size:14px; color:#888"> <p>本小册面向Magento2以上版本,书代码及示例兼容magento2.0-2.4版本。涵盖了magento前端开发,后端开发,magento2主题,magento2重写,magento2 layout,magento2控制器,magento2 block等相关内容,带领您成为magento开发技术专家。</p> </div> <hr><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">以下是构造一个Magento 2 API请求的示例代码:</p><pre class="brush:as3;toolbar:false"> //&nbsp;设置Magento&nbsp;2&nbsp;API的基本URL和访问令牌 $baseUrl&nbsp;=&nbsp;&#39;https://example.com&#39;; $accessToken&nbsp;=&nbsp;&#39;your_access_token&#39;; //&nbsp;创建请求参数 $requestData&nbsp;=&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&#39;entity&#39;&nbsp;=&gt;&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#39;name&#39;&nbsp;=&gt;&nbsp;&#39;New&nbsp;Product&#39;, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#39;sku&#39;&nbsp;=&gt;&nbsp;&#39;new-product-sku&#39;, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#39;price&#39;&nbsp;=&gt;&nbsp;10.00, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#39;status&#39;&nbsp;=&gt;&nbsp;1, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#39;visibility&#39;&nbsp;=&gt;&nbsp;4, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#39;type_id&#39;&nbsp;=&gt;&nbsp;&#39;simple&#39;, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#39;attribute_set_id&#39;&nbsp;=&gt;&nbsp;4, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#39;weight&#39;&nbsp;=&gt;&nbsp;0.5, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#39;custom_attributes&#39;&nbsp;=&gt;&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#39;attribute_code&#39;&nbsp;=&gt;&nbsp;&#39;description&#39;, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#39;value&#39;&nbsp;=&gt;&nbsp;&#39;New&nbsp;product&nbsp;description&#39; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#39;attribute_code&#39;&nbsp;=&gt;&nbsp;&#39;manufacturer&#39;, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#39;value&#39;&nbsp;=&gt;&nbsp;&#39;New&nbsp;product&nbsp;manufacturer&#39; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;] &nbsp;&nbsp;&nbsp;&nbsp;] ]; //&nbsp;创建cURL资源 $ch&nbsp;=&nbsp;curl_init(); //&nbsp;设置请求URL和请求头 curl_setopt($ch,&nbsp;CURLOPT_URL,&nbsp;$baseUrl&nbsp;.&nbsp;&#39;/rest/V1/products&#39;); curl_setopt($ch,&nbsp;CURLOPT_HTTPHEADER,&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&#39;Authorization:&nbsp;Bearer&nbsp;&#39;&nbsp;.&nbsp;$accessToken, &nbsp;&nbsp;&nbsp;&nbsp;&#39;Content-Type:&nbsp;application/json&#39; ]); //&nbsp;设置请求体 curl_setopt($ch,&nbsp;CURLOPT_POST,&nbsp;true); curl_setopt($ch,&nbsp;CURLOPT_POSTFIELDS,&nbsp;json_encode($requestData)); //&nbsp;执行请求并获取响应结果 curl_setopt($ch,&nbsp;CURLOPT_RETURNTRANSFER,&nbsp;true); $response&nbsp;=&nbsp;curl_exec($ch); //&nbsp;检查请求是否成功 if&nbsp;($response&nbsp;===&nbsp;false)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;&#39;Error:&nbsp;&#39;&nbsp;.&nbsp;curl_error($ch); }&nbsp;else&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;打印响应结果 &nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;$response; } //&nbsp;关闭cURL资源 curl_close($ch);</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">在此示例中,我们首先设置了Magento 2 API的基本URL和访问令牌。然后,我们创建了一个包含新产品信息的请求参数。接下来,我们使用cURL库创建了一个cURL资源,并设置了请求URL和请求头。我们还设置了请求体,将请求方法设置为POST。最后,我们执行请求,并打印响应结果。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p3" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;PingFang SC&quot;; white-space: normal;">请注意,这只是一个简单的示例。在实际使用中,您需要根据自己的需求和<span class="s1" style="font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: normal; font-family: &quot;Helvetica Neue&quot;;">API</span>文档来构造请求参数和解析响应结果。</p><p><br/></p>

magento2中的API安全以及代码示例

<h5 style="color:red;">系统学习magento二次开发,推荐小册:<a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank">《Magento中文全栈二次开发 》</a></h5> <div class="image-container"> <p> <a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank"> <img src="https://www.maxiaoke.com/uploads/images/20230218/bb9c82995c24d1105676e02f373755f5.jpg" alt="Magento中文全栈二次开发"> </a> </p> </div> <div class="text-container" style="font-size:14px; color:#888"> <p>本小册面向Magento2以上版本,书代码及示例兼容magento2.0-2.4版本。涵盖了magento前端开发,后端开发,magento2主题,magento2重写,magento2 layout,magento2控制器,magento2 block等相关内容,带领您成为magento开发技术专家。</p> </div> <hr><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">在Magento 2中,API的安全性是通过访问令牌(Access Token)和角色权限控制实现的。以下是一个示例代码,演示如何使用访问令牌来访问Magento 2 API并确保安全性:</p><pre class="brush:as3;toolbar:false">//&nbsp;设置Magento&nbsp;2&nbsp;API的基本URL和访问令牌 $baseUrl&nbsp;=&nbsp;&#39;https://example.com&#39;; $accessToken&nbsp;=&nbsp;&#39;your_access_token&#39;; //&nbsp;创建cURL资源 $ch&nbsp;=&nbsp;curl_init(); //&nbsp;设置请求URL和请求头 curl_setopt($ch,&nbsp;CURLOPT_URL,&nbsp;$baseUrl&nbsp;.&nbsp;&#39;/rest/V1/customers/1&#39;); curl_setopt($ch,&nbsp;CURLOPT_HTTPHEADER,&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&#39;Authorization:&nbsp;Bearer&nbsp;&#39;&nbsp;.&nbsp;$accessToken, &nbsp;&nbsp;&nbsp;&nbsp;&#39;Content-Type:&nbsp;application/json&#39; ]); //&nbsp;执行请求并获取响应结果 curl_setopt($ch,&nbsp;CURLOPT_RETURNTRANSFER,&nbsp;true); $response&nbsp;=&nbsp;curl_exec($ch); //&nbsp;检查请求是否成功 if&nbsp;($response&nbsp;===&nbsp;false)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;&#39;Error:&nbsp;&#39;&nbsp;.&nbsp;curl_error($ch); }&nbsp;else&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;打印响应结果 &nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;$response; } //&nbsp;关闭cURL资源 curl_close($ch);</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">在此示例中,我们设置了Magento 2 API的基本URL和访问令牌,并将访问令牌作为请求头的Authorization字段发送。在Magento 2中,您可以使用令牌管理页面来管理访问令牌。令牌管理页面允许您创建、编辑和删除令牌,并为每个令牌分配特定的角色。角色是一组API操作的集合,可以控制令牌的访问权限。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p3" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;PingFang SC&quot;; white-space: normal;">请注意,在实际使用中,您应该采取其他安全措施来保护您的<span class="s1" style="font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: normal; font-family: &quot;Helvetica Neue&quot;;">Magento 2 API</span>。例如,您可以使用<span class="s1" style="font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: normal; font-family: &quot;Helvetica Neue&quot;;">SSL</span>证书来加密通信,并使用<span class="s1" style="font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: normal; font-family: &quot;Helvetica Neue&quot;;">IP</span>限制来限制<span class="s1" style="font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: normal; font-family: &quot;Helvetica Neue&quot;;">API</span>访问。另外,您还应该确保您的<span class="s1" style="font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: normal; font-family: &quot;Helvetica Neue&quot;;">Magento 2</span>实例已经更新到最新版本,并采用最佳实践来保护您的服务器免受安全威胁。</p><p><br/></p>

magento2中的配置 TinyMCE 编辑器

<h5 style="color:red;">系统学习magento二次开发,推荐小册:<a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank">《Magento中文全栈二次开发 》</a></h5> <div class="image-container"> <p> <a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank"> <img src="https://www.maxiaoke.com/uploads/images/20230218/bb9c82995c24d1105676e02f373755f5.jpg" alt="Magento中文全栈二次开发"> </a> </p> </div> <div class="text-container" style="font-size:14px; color:#888"> <p>本小册面向Magento2以上版本,书代码及示例兼容magento2.0-2.4版本。涵盖了magento前端开发,后端开发,magento2主题,magento2重写,magento2 layout,magento2控制器,magento2 block等相关内容,带领您成为magento开发技术专家。</p> </div> <hr><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">在Magento 2中,可以使用TinyMCE编辑器来提供更丰富的文本编辑功能。以下是如何配置TinyMCE编辑器以及如何在Magento 2中使用它的代码示例:</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">在Magento 2中配置TinyMCE编辑器</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">首先,在Magento 2中启用WYSIWYG编辑器。打开Magento 2的后台,依次单击“Stores”&gt;“Configuration”&gt;“General”&gt;“Content Management”,在“WYSIWYG Options”下,将“Enable WYSIWYG Editor”设置为“Yes”。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">然后,将TinyMCE作为默认编辑器。在“WYSIWYG Options”下,将“Editor”设置为“TinyMCE”。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">最后,根据需要进行其他设置,例如设置编辑器的宽度和高度,启用/禁用某些按钮等。完成后,单击“Save Config”保存更改。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">在Magento 2中使用TinyMCE编辑器</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">在Magento 2中使用TinyMCE编辑器非常简单。只需在需要使用编辑器的文本区域中添加以下代码:</p><pre class="brush:as3;toolbar:false">&lt;textarea&nbsp;id=&quot;editor&quot;&nbsp;name=&quot;editor&quot;&gt;&lt;/textarea&gt; &lt;script&gt; require([&#39;jquery&#39;,&#39;mage/adminhtml/wysiwyg/tiny_mce/setup&#39;],&nbsp;function($){ &nbsp;&nbsp;&nbsp;tinyMceWysiwygSetup.prototype.baseStaticUrl&nbsp;=&nbsp;$(&#39;#base-url&#39;).attr(&#39;href&#39;)+&#39;pub/static/frontend/Magento/luma/en_US/Magento_Cms/&#39;; &nbsp;&nbsp;&nbsp;tinyMceWysiwygSetup.prototype.baseMediaUrl&nbsp;=&nbsp;$(&#39;#base-url&#39;).attr(&#39;href&#39;)+&#39;pub/media/&#39;; &nbsp;&nbsp;&nbsp;tinyMceWysiwygSetup.prototype.settings&nbsp;=&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;theme_advanced_buttons1&nbsp;:&nbsp;&#39;bold,italic,|,justifyleft,justifycenter,justifyright,|,bullist,numlist,|,outdent,indent,|,undo,redo,|,link,unlink,|,image,media&#39;, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;theme_advanced_buttons2&nbsp;:&nbsp;&#39;&#39;, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;theme_advanced_buttons3&nbsp;:&nbsp;&#39;&#39;, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;theme_advanced_buttons4&nbsp;:&nbsp;&#39;&#39;, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;theme_advanced_statusbar_location&nbsp;:&nbsp;&#39;bottom&#39;, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;theme_advanced_resizing&nbsp;:&nbsp;true, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;theme_advanced_resize_horizontal&nbsp;:&nbsp;false, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;apply_source_formatting&nbsp;:&nbsp;true, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;width&nbsp;:&nbsp;&#39;100%&#39;, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;height&nbsp;:&nbsp;&#39;250px&#39; &nbsp;&nbsp;&nbsp;}; &nbsp;&nbsp;&nbsp;new&nbsp;tinyMceWysiwygSetup(&#39;editor&#39;,&nbsp;{}); }); &lt;/script&gt;</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">这将在指定的textarea元素上初始化TinyMCE编辑器,并设置其选项。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">请注意,此代码假定您使用Magento 2的默认主题(Luma)。如果您使用的是自定义主题,请相应地更改代码中的路径。此外,如果您要在多个文本区域中使用TinyMCE编辑器,请为每个文本区域指定唯一的id和name。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p><br/></p>