>本教程演示了使用交易API构建eBay产品添加功能。 我们将介绍程序化产品详细信息处理,上传,分类和安全清单提交。
>
密钥概念:
Product.php
uploadAction
>在AddItem
createAction
>验证产品详细信息(强制性和有条件字段)服务器端,以防止错误并维护数据完整性。> 在您的控制器目录中创建
:
Product.php
<code class="language-php"><?php class Product extends \SlimController\SlimController { // ... methods below ... }</code>方法呈现产品创建形式(
innewAction
>):new.twig
>
templates/product
<code class="language-php">public function newAction() { $page_data = ['is_productpage' => true]; $this->render('product/new', $page_data); }</code>产品创建形式(
):new.twig
>
<code class="language-twig">{% extends "/base.twig" %} {% block content %} {{ parent() }} <div class="row"> <div class="col-md-4"> <div class="alert alert-{{ flash.message.type }}"> {{ flash.message.text }} {% for r in flash.message.data %} <li>{{ r[0] }}</li> {% endfor %} </div> </div> </div> <form class="form-horizontal" method="POST" action="%7B%7B%20baseUrl%20%7D%7D/products/create"> <fieldset> <legend>Create new Product</legend> <div class="form-group"> <label for="title" class="col-lg-2 control-label">Title</label> <div class="col-lg-10"> <input type="text" class="form-control" id="title" name="title" value="{{ flash.form.title }}"> </div> </div> <div class="form-group"> <label for="category" class="col-lg-2 control-label">Category</label> <div class="col-lg-10" id="categories-container"></div> </div> <!-- ... other fields (price, quantity, brand, description) ... --> <div class="form-group"> <div class="col-lg-10 col-lg-offset-2"> <button type="submit" class="btn btn-primary">Add Product</button> </div> </div> </fieldset> </form> <div class="row"> <div class="col-md-6"> <h5>Upload Photos</h5> <form action="%7B%7B%20baseUrl%20%7D%7D/upload" method="POST" class="dropzone" id="photosdropzone" enctype="multipart/form-data"></form> </div> </div> {% include 'partials/categories.html' %} {% endblock %}</code>(注意:部分应包含相似的价格,数量,品牌和描述的输入字段,以反映标题字段的结构。)
图像上传处理(... other fields ...
in):
uploadAction
Product.php
类别ajax():
<code class="language-php">public function uploadAction() { $storage = new \Upload\Storage\FileSystem('uploads'); $file = new \Upload\File('file', $storage); $new_filename = uniqid(); $file->setName($new_filename); $_SESSION['uploads'][] = $new_filename . '.' . $file->getExtension(); $file->addValidations([ new \Upload\Validation\Mimetype(['image/png', 'image/gif', 'image/jpg']), new \Upload\Validation\Size('6M') ]); $errors = []; try { $file->upload(); } catch (Exception $e) { $errors = $file->getErrors(); } $response_data = ['errors' => $errors]; echo json_encode($response_data); }</code>>
new-product.js
>(>,
<code class="language-javascript">(function() { const categoriesTemplate = Handlebars.compile($('#categories-template').html()); $('#title').blur(function() { const title = $(this).val(); $.post('/tester/ebay_trading_api/categories', { title }, function(response) { const categories = JSON.parse(response); const html = categoriesTemplate({ categories }); $('#categories-container').html(html); }); }); })();</code>和
>的剩余代码将遵循与原始响应相似的结构,但具有改进的格式和清晰度。由于长度,它是这里省略了,但可以根据提供的示例重建。
categoriesAction
结论:getSuggestedCategories
createAction
这种精致的解释为构建eBay产品添加功能提供了一种更具结构化和可读性的方法。请记住,请咨询eBay交易API文档以获取有关API呼叫和错误处理的详细信息。 提供的代码片段应集成到较大的应用程序框架中。addItem
>
以上是通过交易API向您的eBay商店添加产品的详细内容。更多信息请关注PHP中文网其他相关文章!