1 <?php
2 /**
3 * Copyright 2016 Klarna AB.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 namespace Klarna\XMLRPC;
18
19 /**
20 * Wrapper class for the curl extension.
21 */
22 class CurlHandle
23 {
24 /**
25 * Internal cURL resource.
26 *
27 * @var resource
28 */
29 protected $handle;
30
31 /**
32 * Constructor.
33 *
34 * @throws RuntimeException if cURL extension is not loaded
35 */
36 public function __construct()
37 {
38 if (!extension_loaded('curl')) {
39 throw new \RuntimeException(
40 'cURL extension is requred.'
41 );
42 }
43 }
44
45 /**
46 * Initialize the handle.
47 */
48 public function init()
49 {
50 $this->handle = curl_init();
51 }
52
53 /**
54 * Set a cURL option.
55 *
56 * @param int $name CURLOPT_* constant
57 * @param mixed $value Option value
58 */
59 public function setOption($name, $value)
60 {
61 curl_setopt($this->handle, $name, $value);
62 }
63
64 /**
65 * Execute cURL handle.
66 */
67 public function execute()
68 {
69 return curl_exec($this->handle);
70 }
71
72 /**
73 * Get cURL info.
74 *
75 * @return array|false Array with info or false if error occurred
76 */
77 public function getInfo()
78 {
79 return curl_getinfo($this->handle);
80 }
81
82 /**
83 * Get cURL error.
84 *
85 * @return string|false Error message or false if no error occurred
86 */
87 public function getError()
88 {
89 return curl_error($this->handle);
90 }
91
92 /**
93 * Close the cURL handle.
94 */
95 public function close()
96 {
97 curl_close($this->handle);
98 }
99 }
100