prestashopwebservice API 使用表单将新客户发布到数据库
prestashopwebservice API posting a new customer to the database by using a form
首先,我不知道标题是否最好,但我已尽力描述它。
所以我目前正在从事一个使用 PrestashopWebserviceLibrary 的项目,我只是很难过,使用 prestashop API。我遇到了一个我无法解决的问题。
所以objective是给post一个新客户通过一个表格到数据库。该表单有一个与 angular 一起使用的提交按钮,因此 ng-click="function()"
。然后该函数从表单中收集所有变量,将其放入 $.ajax POST
请求并将其发送到文件 set.php
。在这个文件中,我们将请求数据库插入所有内容。该文件使用 XML
.
我认为这会让它更容易理解:
形式
<form ng-controller="CustomerController" class="bottomborderinput margin-t-25 col-md-11 col-lg-10 col-xl-8">
<div class="form-group justify-content-between">
<label class="padding-r-30 custom-radio">{{ 'Aanhef' | translate }}</label>
<label class="custom-control custom-radio">
<input id="radio1" name="radio" type="radio" class="custom-control-input">
<span class="custom-control-indicator"></span>
<span class="custom-control-description">{{ 'Dhr.' | translate }}</span>
</label>
<label class="custom-control custom-radio">
<input id="radio2" name="radio" type="radio" class="custom-control-input">
<span class="custom-control-indicator"></span>
<span class="custom-control-description">{{ 'Mevr.' | translate }}</span>
</label>
</div>
<div class="form-group row justify-content-between">
<div class="col-6">
<label class="d-flex">{{ 'Voornaam' | translate }}</label>
<input type="text" name="firstname" id="firstname" class="form-control" aria-describedby="" value="">
</div>
<div class="col-6">
<label class="d-flex">{{ 'Achternaam' | translate }}</label>
<input type="text" name="lastname" class="form-control" aria-describedby="" value="">
</div>
</div>
<div class="form-group row justify-content-between">
<div class="col-12">
<label class="d-flex">{{ 'Straat en huisnummer' | translate }}</label>
<input type="text" class="form-control" name="residential" aria-describedby="" value="">
</div>
</div>
<div class="form-group row justify-content-between">
<div class="col-6">
<label class="d-flex">{{ 'Postcode' | translate }}</label>
<input type="text" name="area_code" class="form-control" aria-describedby="" value="">
</div>
<div class="col-6">
<label class="d-flex">{{ 'Plaats' | translate }}</label>
<input type="text" name="city" class="form-control" aria-describedby="" value="">
</div>
</div>
<div class="form-group row justify-content-between">
<div class="col-6">
<label class="d-flex">{{ 'Land' | translate }}</label>
<select class="form-control" name="country">
<option value="Netherlands">Nederland</option>
</select>
</div>
<div class="col-6">
<label class="d-flex">{{ 'Telefoon nr.' | translate }}</label>
<input type="text" name="phone" class="form-control" aria-describedby="" value="">
</div>
</div>
<div class="form-group row">
<div class="col-6">
<label class="d-flex">{{ 'Bedrijfsnaam' | translate }}</label>
<input type="text" name="company_name" class="form-control" aria-describedby="" value="">
</div>
<div class="col-6">
<label class="d-flex">{{ 'BTW nummer' | translate }}</label>
<input type="text" class="form-control" aria-describedby="" value="">
</div>
</div>
<div class="form-group row">
<div class="col-12">
<label class="d-flex">{{ 'Klantgroep' | translate }}</label>
<select name="customer_group" class="form-control">
<option value="Default">{{ 'Standaard' | translate }}</option>
</select>
</div>
</div>
<div class="form-group text-center margin-b-10">
<button ng-click="RegisterCustomer()" type="submit" name="register_customer" class="defaultbutton mediumbutton">
<svg class="align-middle float-left " width="27" height="25" viewBox="13 12 27 21" xmlns="http://www.w3.org/2000/svg">
<path d="M23.065 32.28c-.287 0-.575-.124-.8-.373l-8.182-9.057c-.47-.52-.494-1.394-.052-1.95.443-.554 1.183-.58 1.653-.06l7.365 8.153 14.395-16.6c.46-.532 1.2-.523 1.652.022.452.544.443 1.416-.018 1.95L23.882 31.886c-.227.262-.522.393-.817.393z" fill="#FFF" fill-rule="evenodd"/>
</svg>{{ 'Klant toevoegen' | translate }}
</button>
</div>
</form>
app.js中的函数
myApp.controller('CustomerController', function($scope, $http){
$http.get('config/customers.php').then(function(response){
$scope.customers = response.data.customers.customer
});
$scope.RegisterCustomer = function() {
var pre = $('radio').val();
var firstname = $('firstname').val();
var lastname = $('lastname').val();
var residential = $('residential').val();
var area_code = $('area_code').val();
var city = $('city').val();
var country = $('country').val();
var phone = $('phone').val();
var company_name = $('company_name').val();
var customer_group = $('customer_group').val();
$scope.ajax({
type:'POST',
data:{pre:pre, firstname:firstname, lastname:lastname, residential:residential, area_code:area_code, city:city, country:country, phone:phone, company_name:company_name, customer_group:customer_group},
url:"config/set.php" // PHP Page URL To php code saving the input to the database
})
};
});
set.php
<html><head><title></title></head><body>
<?php
// Here we define constants /!\ You need to replace this parameters
define('DEBUG', true);
define('PS_SHOP_PATH', 'Example.nl/prestademo');
define('PS_WS_AUTH_KEY', 'example123');
require_once "PSWebServiceLibrary.php";
$firstname = $_POST['firstname'];
// Here we make the WebService Call
$psXML = <<<XML
<prestashop>
<customer>
<id/>
<id_shop_group>2</id_shop_group>
<id_shop>1</id_shop>
<id_gender>1</id_gender>
<id_default_group>3</id_default_group>
<id_lang>1</id_lang>
<id_risk>0</id_risk>
<company>123</company>
<siret/>
<ape/>
<firstname><![CDATA[. $firstname .]]></firstname>
<lastname>Test</lastname>
<email>Test2@email.com</email>
<passwd>asd</passwd>
<lastpasswd_gen/>
<birthday/>
<newsletter>0</newsletter>
<ip_registration_newsletter/>
<newsletter_date_add/>
<optin>0</optin>
<website>easywebs.nl</website>
<outstanding_allow_amount/>
<show_public_prices>0</show_public_prices>
<max_payment_days>0</max_payment_days>
<secure_key/>
<note/>
<active>1</active>
<is_guest>0</is_guest>
<deleted>0</deleted>
<date_add/>
<date_upd/>
<reset_password_token/>
<reset_password_validity/>
</customer>
</prestashop>
XML;
$webService = new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, DEBUG);
$xml = new SimpleXMLElement($psXML);
$opt = array('resource' => 'customers');
$opt['postXml'] = $xml->asXML();
$xml = $webService->add($opt);
?>
调试响应。
RETURN HTTP BODY
<?xml version="1.0" encoding="UTF-8"?>
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
<customer>
<id><![CDATA[34]]></id>
<id_default_group xlink:href="https://example.nl/prestademo/api/groups/3"><![CDATA[3]]></id_default_group>
<id_lang xlink:href="https://example.nl/prestademo/api/languages/1"><![CDATA[1]]></id_lang>
<newsletter_date_add></newsletter_date_add>
<ip_registration_newsletter></ip_registration_newsletter>
<last_passwd_gen><![CDATA[2017-10-12 04:40:08]]></last_passwd_gen>
<secure_key><![CDATA[c1d6050c9869552de049868e8029374a]]></secure_key>
<deleted><![CDATA[0]]></deleted>
<passwd><![CDATA[y$CyIJLPNvX1.yx7xRiyWMtegtSneyf8nNPXs3WklFdPq6BT0EzlgTi]]></passwd>
<lastname><![CDATA[Test]]></lastname>
<firstname><![CDATA[. .]]></firstname>
<email><![CDATA[Test2@email.com]]></email>
<id_gender><![CDATA[1]]></id_gender>
<birthday></birthday>
<newsletter><![CDATA[0]]></newsletter>
<optin><![CDATA[0]]></optin>
<website><![CDATA[easywebs.nl]]></website>
<company><![CDATA[123]]></company>
<siret></siret>
<ape></ape>
<outstanding_allow_amount></outstanding_allow_amount>
<show_public_prices><![CDATA[0]]></show_public_prices>
<id_risk><![CDATA[0]]></id_risk>
<max_payment_days><![CDATA[0]]></max_payment_days>
<active><![CDATA[1]]></active>
<note></note>
<is_guest><![CDATA[0]]></is_guest>
<id_shop><![CDATA[1]]></id_shop>
<id_shop_group><![CDATA[2]]></id_shop_group>
<date_add><![CDATA[2017-10-12 10:40:08]]></date_add>
<date_upd><![CDATA[2017-10-12 10:40:08]]></date_upd>
<reset_password_token></reset_password_token>
<reset_password_validity></reset_password_validity>
<associations>
<groups nodeType="group" api="groups">
<group xlink:href="https://sdwebdesign.nl/prestademo/api/groups/3">
<id><![CDATA[3]]></id>
</group>
</groups>
</associations>
</customer>
</prestashop>
如您所见,我正在尝试将变量 $firstname
作为 posted 项通过 XML
传递,但遗憾的是 return 是空的。所以是的 post 确实有效,项目确实被插入到数据库中,除了我的 post 变量。
PS:app.js
文件中的$scope.ajax
是一个测试。我也试过 $.ajax
,两者似乎都工作正常。
进度编辑
将变量 $firstname = $_POST['firstname']
更改为 $firstname = 'test';有效。我也试过在 XML 文件中使用 <firstname>{$firstname}</firstname>
也能正常工作。所以错误是在 POST 请求中的某个地方。
错误是 500 错误,响应为:
<message><![CDATA[Validation error: "Property Customer->firstname is empty."]]></message>
提前感谢您的帮助!
所以我发现了问题。
如进度编辑部分所述,它与post有关。
问题是:
var firstname = $('firstname').val();
一个空值。所以将 .val();
更改为:
$('input[name="setFirstname"]').val();
问题解决了。
实际上这是一个愚蠢的问题,因为我可以自己看到它。
但是对于遇到同样问题的任何人,我将留下这个答案。
创建表单并设置:
<input type="text" name="setFirstname" id="firstname" class="form-control" aria-describedby="" value="">
输入姓名:
<input name="Javascript .val">
因此您可以使用以下方法获取输入值:
var firstname = $('input[name="setFirstname"]').val();
然后 $.ajax
请求和 set.php
确实获得了传递的值。
干杯!
首先,我不知道标题是否最好,但我已尽力描述它。
所以我目前正在从事一个使用 PrestashopWebserviceLibrary 的项目,我只是很难过,使用 prestashop API。我遇到了一个我无法解决的问题。
所以objective是给post一个新客户通过一个表格到数据库。该表单有一个与 angular 一起使用的提交按钮,因此 ng-click="function()"
。然后该函数从表单中收集所有变量,将其放入 $.ajax POST
请求并将其发送到文件 set.php
。在这个文件中,我们将请求数据库插入所有内容。该文件使用 XML
.
我认为这会让它更容易理解:
形式
<form ng-controller="CustomerController" class="bottomborderinput margin-t-25 col-md-11 col-lg-10 col-xl-8">
<div class="form-group justify-content-between">
<label class="padding-r-30 custom-radio">{{ 'Aanhef' | translate }}</label>
<label class="custom-control custom-radio">
<input id="radio1" name="radio" type="radio" class="custom-control-input">
<span class="custom-control-indicator"></span>
<span class="custom-control-description">{{ 'Dhr.' | translate }}</span>
</label>
<label class="custom-control custom-radio">
<input id="radio2" name="radio" type="radio" class="custom-control-input">
<span class="custom-control-indicator"></span>
<span class="custom-control-description">{{ 'Mevr.' | translate }}</span>
</label>
</div>
<div class="form-group row justify-content-between">
<div class="col-6">
<label class="d-flex">{{ 'Voornaam' | translate }}</label>
<input type="text" name="firstname" id="firstname" class="form-control" aria-describedby="" value="">
</div>
<div class="col-6">
<label class="d-flex">{{ 'Achternaam' | translate }}</label>
<input type="text" name="lastname" class="form-control" aria-describedby="" value="">
</div>
</div>
<div class="form-group row justify-content-between">
<div class="col-12">
<label class="d-flex">{{ 'Straat en huisnummer' | translate }}</label>
<input type="text" class="form-control" name="residential" aria-describedby="" value="">
</div>
</div>
<div class="form-group row justify-content-between">
<div class="col-6">
<label class="d-flex">{{ 'Postcode' | translate }}</label>
<input type="text" name="area_code" class="form-control" aria-describedby="" value="">
</div>
<div class="col-6">
<label class="d-flex">{{ 'Plaats' | translate }}</label>
<input type="text" name="city" class="form-control" aria-describedby="" value="">
</div>
</div>
<div class="form-group row justify-content-between">
<div class="col-6">
<label class="d-flex">{{ 'Land' | translate }}</label>
<select class="form-control" name="country">
<option value="Netherlands">Nederland</option>
</select>
</div>
<div class="col-6">
<label class="d-flex">{{ 'Telefoon nr.' | translate }}</label>
<input type="text" name="phone" class="form-control" aria-describedby="" value="">
</div>
</div>
<div class="form-group row">
<div class="col-6">
<label class="d-flex">{{ 'Bedrijfsnaam' | translate }}</label>
<input type="text" name="company_name" class="form-control" aria-describedby="" value="">
</div>
<div class="col-6">
<label class="d-flex">{{ 'BTW nummer' | translate }}</label>
<input type="text" class="form-control" aria-describedby="" value="">
</div>
</div>
<div class="form-group row">
<div class="col-12">
<label class="d-flex">{{ 'Klantgroep' | translate }}</label>
<select name="customer_group" class="form-control">
<option value="Default">{{ 'Standaard' | translate }}</option>
</select>
</div>
</div>
<div class="form-group text-center margin-b-10">
<button ng-click="RegisterCustomer()" type="submit" name="register_customer" class="defaultbutton mediumbutton">
<svg class="align-middle float-left " width="27" height="25" viewBox="13 12 27 21" xmlns="http://www.w3.org/2000/svg">
<path d="M23.065 32.28c-.287 0-.575-.124-.8-.373l-8.182-9.057c-.47-.52-.494-1.394-.052-1.95.443-.554 1.183-.58 1.653-.06l7.365 8.153 14.395-16.6c.46-.532 1.2-.523 1.652.022.452.544.443 1.416-.018 1.95L23.882 31.886c-.227.262-.522.393-.817.393z" fill="#FFF" fill-rule="evenodd"/>
</svg>{{ 'Klant toevoegen' | translate }}
</button>
</div>
</form>
app.js中的函数
myApp.controller('CustomerController', function($scope, $http){
$http.get('config/customers.php').then(function(response){
$scope.customers = response.data.customers.customer
});
$scope.RegisterCustomer = function() {
var pre = $('radio').val();
var firstname = $('firstname').val();
var lastname = $('lastname').val();
var residential = $('residential').val();
var area_code = $('area_code').val();
var city = $('city').val();
var country = $('country').val();
var phone = $('phone').val();
var company_name = $('company_name').val();
var customer_group = $('customer_group').val();
$scope.ajax({
type:'POST',
data:{pre:pre, firstname:firstname, lastname:lastname, residential:residential, area_code:area_code, city:city, country:country, phone:phone, company_name:company_name, customer_group:customer_group},
url:"config/set.php" // PHP Page URL To php code saving the input to the database
})
};
});
set.php
<html><head><title></title></head><body>
<?php
// Here we define constants /!\ You need to replace this parameters
define('DEBUG', true);
define('PS_SHOP_PATH', 'Example.nl/prestademo');
define('PS_WS_AUTH_KEY', 'example123');
require_once "PSWebServiceLibrary.php";
$firstname = $_POST['firstname'];
// Here we make the WebService Call
$psXML = <<<XML
<prestashop>
<customer>
<id/>
<id_shop_group>2</id_shop_group>
<id_shop>1</id_shop>
<id_gender>1</id_gender>
<id_default_group>3</id_default_group>
<id_lang>1</id_lang>
<id_risk>0</id_risk>
<company>123</company>
<siret/>
<ape/>
<firstname><![CDATA[. $firstname .]]></firstname>
<lastname>Test</lastname>
<email>Test2@email.com</email>
<passwd>asd</passwd>
<lastpasswd_gen/>
<birthday/>
<newsletter>0</newsletter>
<ip_registration_newsletter/>
<newsletter_date_add/>
<optin>0</optin>
<website>easywebs.nl</website>
<outstanding_allow_amount/>
<show_public_prices>0</show_public_prices>
<max_payment_days>0</max_payment_days>
<secure_key/>
<note/>
<active>1</active>
<is_guest>0</is_guest>
<deleted>0</deleted>
<date_add/>
<date_upd/>
<reset_password_token/>
<reset_password_validity/>
</customer>
</prestashop>
XML;
$webService = new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, DEBUG);
$xml = new SimpleXMLElement($psXML);
$opt = array('resource' => 'customers');
$opt['postXml'] = $xml->asXML();
$xml = $webService->add($opt);
?>
调试响应。
RETURN HTTP BODY
<?xml version="1.0" encoding="UTF-8"?>
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
<customer>
<id><![CDATA[34]]></id>
<id_default_group xlink:href="https://example.nl/prestademo/api/groups/3"><![CDATA[3]]></id_default_group>
<id_lang xlink:href="https://example.nl/prestademo/api/languages/1"><![CDATA[1]]></id_lang>
<newsletter_date_add></newsletter_date_add>
<ip_registration_newsletter></ip_registration_newsletter>
<last_passwd_gen><![CDATA[2017-10-12 04:40:08]]></last_passwd_gen>
<secure_key><![CDATA[c1d6050c9869552de049868e8029374a]]></secure_key>
<deleted><![CDATA[0]]></deleted>
<passwd><![CDATA[y$CyIJLPNvX1.yx7xRiyWMtegtSneyf8nNPXs3WklFdPq6BT0EzlgTi]]></passwd>
<lastname><![CDATA[Test]]></lastname>
<firstname><![CDATA[. .]]></firstname>
<email><![CDATA[Test2@email.com]]></email>
<id_gender><![CDATA[1]]></id_gender>
<birthday></birthday>
<newsletter><![CDATA[0]]></newsletter>
<optin><![CDATA[0]]></optin>
<website><![CDATA[easywebs.nl]]></website>
<company><![CDATA[123]]></company>
<siret></siret>
<ape></ape>
<outstanding_allow_amount></outstanding_allow_amount>
<show_public_prices><![CDATA[0]]></show_public_prices>
<id_risk><![CDATA[0]]></id_risk>
<max_payment_days><![CDATA[0]]></max_payment_days>
<active><![CDATA[1]]></active>
<note></note>
<is_guest><![CDATA[0]]></is_guest>
<id_shop><![CDATA[1]]></id_shop>
<id_shop_group><![CDATA[2]]></id_shop_group>
<date_add><![CDATA[2017-10-12 10:40:08]]></date_add>
<date_upd><![CDATA[2017-10-12 10:40:08]]></date_upd>
<reset_password_token></reset_password_token>
<reset_password_validity></reset_password_validity>
<associations>
<groups nodeType="group" api="groups">
<group xlink:href="https://sdwebdesign.nl/prestademo/api/groups/3">
<id><![CDATA[3]]></id>
</group>
</groups>
</associations>
</customer>
</prestashop>
如您所见,我正在尝试将变量 $firstname
作为 posted 项通过 XML
传递,但遗憾的是 return 是空的。所以是的 post 确实有效,项目确实被插入到数据库中,除了我的 post 变量。
PS:app.js
文件中的$scope.ajax
是一个测试。我也试过 $.ajax
,两者似乎都工作正常。
进度编辑
将变量 $firstname = $_POST['firstname']
更改为 $firstname = 'test';有效。我也试过在 XML 文件中使用 <firstname>{$firstname}</firstname>
也能正常工作。所以错误是在 POST 请求中的某个地方。
错误是 500 错误,响应为:
<message><![CDATA[Validation error: "Property Customer->firstname is empty."]]></message>
提前感谢您的帮助!
所以我发现了问题。
如进度编辑部分所述,它与post有关。
问题是:
var firstname = $('firstname').val();
一个空值。所以将 .val();
更改为:
$('input[name="setFirstname"]').val();
问题解决了。 实际上这是一个愚蠢的问题,因为我可以自己看到它。 但是对于遇到同样问题的任何人,我将留下这个答案。
创建表单并设置:
<input type="text" name="setFirstname" id="firstname" class="form-control" aria-describedby="" value="">
输入姓名:
<input name="Javascript .val">
因此您可以使用以下方法获取输入值:
var firstname = $('input[name="setFirstname"]').val();
然后 $.ajax
请求和 set.php
确实获得了传递的值。
干杯!