SendXMLToWebService.class.php source code
Contents of file
SendXMLToWebService.class.php
1
<?php
2 /*
3 * SendXMLToWebService.class.php
4 *
5 * Copyright 2015 Mario Spada <spadamar@spadamar.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20 * MA 02110-1301, USA.
21 *
22 *
23 */
24
25
26 /**
27 * SendXMLToWebService
28 * This class is for transmitting an http / POST request to Web service
29 * through a XML document
30 *
31 * @author Mario Spada <spadamar@spadamar.com>
32 * @copyright Copyright (c) 2015 Mario Spada
33 * @license http://opensource.org/licenses/GPL-2.0 GNU Public License
34 * @package SendXMLToWebService
35 * @version 0.1.0 2015/11/22
36 */
37
38 class SendXMLToWebService {
39
40 /**
41 * cURL option Connection time out
42 * @var int
43 */
44 public $connecttimeout = 10;
45 /**
46 * cURL option Time out
47 * @var int
48 */
49 public $timeout = 10;
50 /**
51 * cURL option Return transfer
52 * @var boolean
53 */
54 public $returntransfer = true;
55 /**
56 * cURL option SSL Verify Peer
57 * @var boolean
58 */
59 public $ssl_verifypeer = false;
60 /**
61 * cURL option SSL Verify Host
62 * @var boolean
63 */
64 public $ssl_verifyhost = false;
65 /**
66 * cURL option Post
67 * @var boolean
68 */
69 public $post = true;
70 /**
71 * Header Charset
72 * @var string
73 */
74 public $charset = 'utf-8';
75 /**
76 * Header Content-type
77 * @var string
78 */
79 public $contentType = 'text/xml';
80 /**
81 * Enable MD5 encryption for password
82 * @var boolean
83 */
84 public $md5_pass = false;
85 /**
86 * Enable cURL debug
87 * @var boolean
88 */
89 public $debug = true;
90 /**
91 * Set url (private)
92 * @var string
93 */
94 private $_url;
95 /**
96 * Set username (private)
97 * @var string
98 */
99 private $_user = '';
100 /**
101 * Set password (private)
102 * @var string
103 */
104 private $_pass = '';
105 /**
106 * Extra headers (private)
107 * @var array
108 */
109 private $_extraHeaders = array();
110
111 /**
112 * @param string $url url to connect to Web service
113 */
114 public function __construct($url) {
115 $this->_url = $url;
116 $this->_checkRequisites();
117 }
118 /**
119 * Send XML document to Web Service
120 * @param string $post_string string containing XML document
121 * @return string result string, usually in XML format
122 */
123 public function sendXML($post_string) {
124 $psLen = strlen($post_string);
125 $headers = $this->_getHeaders($psLen);
126
127 $soap_do = curl_init();
128 curl_setopt($soap_do, CURLOPT_URL, $this->_url);
129 curl_setopt($soap_do, CURLOPT_CONNECTTIMEOUT, $this->connecttimeout);
130 curl_setopt($soap_do, CURLOPT_TIMEOUT, $this->timeout);
131 curl_setopt($soap_do, CURLOPT_RETURNTRANSFER, $this->returntransfer);
132 curl_setopt($soap_do, CURLOPT_SSL_VERIFYPEER, $this->ssl_verifypeer);
133 curl_setopt($soap_do, CURLOPT_SSL_VERIFYHOST, $this->ssl_verifyhost);
134 curl_setopt($soap_do, CURLOPT_POST, $this->post);
135 curl_setopt($soap_do, CURLOPT_POSTFIELDS, $post_string);
136 curl_setopt($soap_do, CURLOPT_HTTPHEADER, $headers);
137 if (!empty($this->_user)) {
138 curl_setopt($soap_do, CURLOPT_USERPWD, $this->_user.":".$this->_pass);
139 }
140 $result = curl_exec($soap_do);
141 $err = curl_error($soap_do);
142 curl_close ($soap_do);
143 if ($err && $this->debug)
144 echo "ERROR: <pre>".$err."</pre>";
145 return $result;
146
147 }
148 /**
149 * Set username and password
150 * @param string $user username
151 * @param string $pass password
152 */
153 public function setCredentials($user, $pass) {
154 $this->_user = $user;
155 $this->_pass = md5($pass);
156 if ($this->md5_pass) {
157 $this->_pass = md5($this->_pass);
158 }
159 }
160 /**
161 * Set an additional header
162 * @param string $extraHeader a valid header row
163 */
164 public function setExtraHeader($extraHeader) {
165 array_push($this->_extraHeaders,$extraHeader);
166 }
167 /**
168 * Set multiple additional header at once in array format
169 * @param array $extraHeaders an array with multiple header rows
170 */
171 public function setExtraHeaders($extraHeaders) {
172 $this->_extraHeaders = array_merge($this->_extraHeaders,$extraHeaders);
173 }
174 /**
175 * Remove all additional headers
176 */
177 public function resetExtraHeaders() {
178 $this->_extraHeaders = array();
179 }
180 /**
181 * Prints out all additional headers
182 */
183 public function printExtraHeaders() {
184 print_r($this->_extraHeaders);
185 }
186 /**
187 * Compose headers
188 * @param string $psLen length of the XML string
189 */
190 private function _getHeaders($psLen) {
191 $headers = array();
192 array_push($headers,'Content-Type: '.$this->contentType.'; charset='.$this->charset);
193 if (count($this->_extraHeaders) > 0) {
194 $headers = array_merge($headers,$this->_extraHeaders);
195 }
196 array_push($headers,'Content-Length: '.$psLen);
197 return $headers;
198 }
199 /**
200 * Check if cURL and allow_url_fopen are enabled, check url validity
201 */
202 private function _checkRequisites() {
203 if (!function_exists('curl_version'))
204 die("cURL is not enabled!");
205 if(!ini_get('allow_url_fopen'))
206 die("allow_url_fopen is not enabled!");
207 if (!filter_var($this->_url, FILTER_VALIDATE_URL))
208 die("URL is not valid!");
209 return true;
210 }
211
212 }
213 ?>
214