ホームページ >バックエンド開発 >PHPチュートリアル >トレーディングAPIでeBayストアに製品を追加する
このチュートリアルでは、取引APIを使用して機能を追加するeBay製品の構築を示しています。 プログラマティック製品の詳細の処理、アップロード、分類、および安全なリストの提出をカバーします。
効率的なeBay製品の追加のためにトレーディングAPIを活用してください。
Product.php
eBayの要件と互換性のあるSecure Image Uploads(uploadAction
製品の詳細(必須および条件付きフィールド)の検証サーバー側のエラーを防ぎ、データの整合性を維持します。
AddItem
製品作成コントローラーの構築:createAction
メソッドは製品作成フォーム(in
)をレンダリングします:
Product.php
<code class="language-php"><?php class Product extends \SlimController\SlimController { // ... methods below ... }</code>製品作成フォーム(
):newAction
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
画像アップロードハンドリング(in
<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 ...
カテゴリajax(
uploadAction
Product.php
(残りのコードの残りのコードは、
<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>この洗練された説明は、eBay製品の追加機能を構築するための、より構造化されて読みやすいアプローチを提供します。 API呼び出しとエラー処理の詳細については、eBay Trading APIドキュメントを参照してください。 提供されたコードスニペットは、より大きなアプリケーションフレームワークに統合する必要があります。
以上がトレーディングAPIでeBayストアに製品を追加するの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。