Webservice
Open connection to remote service
Open connection to remote service
$Resource = $this->get('webservice', 'netvisor')->getResource('Product');
$Product->Name = 'Tuotenimi';
$Product->UnitPrice = '42,5';
$Product->sendRequest();
Set account preferences for webservice:
webservice:
netvisor:
- name: development
Host: koulutus.netvisor.fi
OrganizationId: 1451145-0
CustomerId: DP_6356_233
CustomerKey: D68C65A5234......7E4811FBFFF
Connect to webservice using account preferences:
$Webservice = $this->get('webservice', 'netvisor', 'development');
$Webservice = $this->get('webservice', 'netvisor', Array(
'Host' => 'koulutus.netvisor.fi',
'OrganizationId' => 'koulutus.netvisor.fi',
'CustomerId' => 'DP_6356_233',
'CustomerKey' => 'D68C65A5234......7E4811FBFFF'
));
If Webservice has resources defined, you can send requests via specifig Resource object that knows what fields the resource has, sets predefined defaults etc. List of Resources are shown in Webservice extensions -list.
Open resource, set values end send data:
$Invoice = $Webservice->getResource('SalesInvoice');
$Invoice->SalesInvoiceDate = $Order->order_date;
$Invoice->SalesInvoiceStatus = 'Open';
Loop through Order -page order lines and add them to SalesInvoice:
foreach ( $Order->order_line as $OrderLine ){
// Create new invoiceline
$InvoiceLine = $Invoice->addInvoiceLine();
// Create new productline to invoicline
$SalesInvoiceProductLine = $InvoiceLine->addSalesInvoiceProductLine();
// Set product data
$SalesInvoiceProductLine->ProductIdentifier = $OrderLine->product->identifier;
$SalesInvoiceProductLine->ProductName = $OrderLine->product->name;
$SalesInvoiceProductLine->ProductUnitPrice = $OrderLine->unit_price;
$SalesInvoiceProductLine->ProductVatPercentage = $OrderLine->unit_price->vat;
$SalesInvoiceProductLine->SalesInvoiceProductLineQuantity = $OrderLine->quantity;
}
Just send request to remote server:
$Invoice->sendRequest();
Send and get response:
$response = $Invoice->getResponse();
Send request and check response for errors:
if ( $Invoice->getSuccess() ){
$response = $Invoice->getResponse();
}
else {
print "VIRHE: ".$Invoice->getStatus()." ".$Invoice->getMessage();
}
Sending custom requests to webservice. NOTE! This may not work with webservices that has Resources defined, because webservice may not be so straight forward to use!
$Request = $Webservice->newRequest('GET', 'invoices', Array(
'incoice_date' => $Order->order_date
));
$Request->setMethod('POST');
$Request->setRoute('invoice');
$Request->addParameter('invoice_status', 'New');
$Request->addParameters(Array(
'invoice_status' => 'New'
);
$Request->setData(Array(
'invoice_status', 'New'
);
$Request->addHeader(Array(
'Content-type' => 'application/json'
);
Just send request to remote server:
$Request->sendRequest();
Send and get response:
$response = $Request->getResponse();
Send request and get response or show error:
if ( $Request->isSuccess() ){
$response = $Request->getResponse();
}
else {
print "ERROR: ".$Request->getStatus()." ".$Request->getCode()." ".$Request->getMessage();
}
Object that defines webservice connection and preferences.
Method | return | Description |
---|---|---|
setDebug(bool) | this | Set debug (renders additional info) |
setTest(bool) | this | Set test (does not send request) |
getTest() | bool | Get TRUE, if test |
getDebug() | bool | Get TRUE, if debug |
getResource(name[,param]) | WebserviceResource | New resource object |
getResousList() | array | Get list of resources defined in webservice |
newRequest(method,route,params,data) | WebserviceRequest | New request object |
Object that defines what is sent and where.
Method | return | Description |
---|---|---|
getLog() | WebserviceLog | Request WebserviceLog -object |
setSaveLog(bool) | this | |
getRequest() | WebserviceRequest | Resource request object |
sendRequest() | bool | Return TRUE if send was success |
getResult() | array | Response data |
getSuccess() | bool | TRUE if send was success |
getStatus() | string | Response status |
getMessage() | string | Response message |
getXmlString() | string | XML string |
Object that defines webservice connection and preferences.
Method | return | Description |
---|---|---|
setDebug(bool) | this | Set debug (renders additional info) |
addParameter(name,value) | this | Set request parameter |
addParameters(array) | this | Add request parameters |
setHeader(name,value) | this | Set request header |
setData(array) | this | Set request data |
sendRequest() | bool | Return TRUE if request sent with no errors |
Request info:
Method | return | Description |
---|---|---|
getDebug() | bool | Get TRUE, if debug |
getLog() | WebserviceLog | WebserviceLog -object |
getMethod() | string | Request HTTP method |
getProtocol() | string | Request http protocol |
getHost() | string | Base url |
getRoute() | string | Request route |
getUrl() | string | Request url |
getParameters() | array | Request parameters |
isParameter(name) | bool | Return true, if parameter with name is defined |
getData() | array | Get request data |
getHeaders() | array | Get Request parameters |
Request response:
Method | return | Description |
---|---|---|
getResponse() | Array | Response data |
getResult() | string | Original response content |
getInfo() | array | Request info (curl_getinfo) |
isSuccess() | bool | Return TRUE if request sent with no errors |
getStatus() | int | Response status code |
getCode() | string | Response message code (some webservices uses) |
getMessage() | string | Response message |
Method | return | Description |
---|---|---|
setTitle(string) | this | Set log title |
setMessage(string) | this | Set log message |