-
Notifications
You must be signed in to change notification settings - Fork 0
/
krak-webservice.php
128 lines (110 loc) · 3.62 KB
/
krak-webservice.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?
/*
* Krak Webservices - A PHP class querying the paid services from Krak
* <http://www.krakwebservices.dk/>.
*
* Krak is a trademark of Eniro. This code is not provided by or has
* any relation to Krak, except for the use of their service.
*
* Copyright 2012 Kristeligt Dagblad, Mikkel Munch Mortensen
* <[email protected]>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* A class for accessing the Krak Webservices, e.g. for looking up
* names and addresses by a phone number. Based on the (faulty) code
* example available from:
* <http://www.krakwebservices.dk/documentation.html>.
*/
class KrakWebservice
{
/*
* Initialize by getting a valid a ticket.
*/
function __construct($username, $password, $product_id)
{
// Ini settings.
ini_set('soap.wsdl_cache_enabled', 1);
ini_set('soap.wsdl_cache_dir', '/tmp');
// Setup various internal stuff.
$this->username = $username;
$this->password = $password;
$this->product_id = $product_id;
$this->locale = 'da-DK';
$this->namespace = 'http://webservice.krak.dk/';
// Get a valid ticket.
$this->ticket = $this->get_ticket();
}
/*
* Asks the Krak Webservice for a ticket to use later on.
*/
function get_ticket()
{
// Prepare ticket params.
$params = array(
'userName' => $this->username,
'password' => $this->password,
'locale' => $this->locale,
);
// Request a ticket.
$client = new SoapClient('http://login.webservice.krak.dk/ticketcentral.asmx?op=GetTicketByUser&wsdl');
$results = $client->GetTicketByUser($params);
// Handle result.
foreach ($results as $res) {
$ticket['ticket'] = $res->ticket;
$ticket['timeout'] = $res->timeout;
break;
}
// Set timeout according to response.
ini_set('soap.wsdl_cache_ttl', $ticket['timeout']);
return $ticket;
}
/*
* Get name and address info by looking up a phone number.
*/
function get_tele_by_tn($number)
{
// Prepare request headers.
$ticket = array( 'ticket' => $this->ticket['ticket'], 'product' => $this->product_id, 'username' => $this->username);
// Set Headers
$SoapHeader[] = new SoapHeader($this->namespace, 'KrakSoapHeader', array('ticket' => $ticket['ticket']));
$SoapHeader[] = new SoapHeader($this->namespace, 'KrakSoapHeader', array('product' => $this->product_id));
$SoapHeader[] = new SoapHeader($this->namespace, 'KrakSoapHeader', array('username' => $this->username));
// Create client and set headers.
$rclient = new SoapClient('http://basicservices.webservice.krak.dk/telesearch.asmx?WSDL');
$rclient->__setSoapHeaders($SoapHeader);
// Prepare request params.
$params = array(
'telephoneNumber' => $number
);
// Request data.
$objdata = $rclient->GetTeleByTn($params);
// Extract resulting data.
foreach ($objdata as $data)
{
if (property_exists($data, 'Tele'))
{
return $data->Tele;
}
}
// This one is for empty results.
return null;
}
/*
* The rest of the web service calls may be implemented (when needed) some day.
* Please get in touch if you do (some of) this.
*/
}
?>