Webservice

Open connection to remote service

Opens connection to remote service using Webservice extension.

Arguments

$name
Name of the webservice in account preferences.
Default: First preference that is found.
$preferences
Connection parameters
If given, $name is the name of the Drive type.

Example

$Resource = $this->get('webservice', 'netvisor')->getResource('Product');
$Product->Name       = 'Tuotenimi';
$Product->UnitPrice  = '42,5';
$Product->sendRequest();

Connection

Set connection

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');

Use custom parameters

$Webservice = $this->get('webservice', 'netvisor', Array(
  'Host'      => 'koulutus.netvisor.fi',
  'OrganizationId'      => 'koulutus.netvisor.fi',
  'CustomerId'      => 'DP_6356_233',
  'CustomerKey'      => 'D68C65A5234......7E4811FBFFF'
));

Sending request with Resource

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.

Create resource and set values

Open resource, set values end send data:

$Invoice = $Webservice->getResource('SalesInvoice');
$Invoice->SalesInvoiceDate = $Order->order_date;
$Invoice->SalesInvoiceStatus = 'Open';

Add multidimensional data

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;
}

Send request

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 Request

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!

Create new request and set values

$Request = $Webservice->newRequest('GET', 'invoices', Array(
    'incoice_date' => $Order->order_date
));

Modify request data

$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'
);

Send request

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();
}

Full methos list

Webservice

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

WebserviceResource

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

WebserviceRequest

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

WebserviceLog

Method return Description
setTitle(string) this Set log title
setMessage(string) this Set log message