|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace SoapClientCurl; |
| 4 | + |
| 5 | +class SoapClientRequest { |
| 6 | + |
| 7 | + private static $handle = null; |
| 8 | + private static $socketTimeout = null; |
| 9 | + private static $defaultHeaders = array(); |
| 10 | + |
| 11 | + private static $auth = array ( |
| 12 | + 'user' => '', |
| 13 | + 'pass' => '', |
| 14 | + 'method' => CURLAUTH_BASIC |
| 15 | + ); |
| 16 | + private static $proxy = array( |
| 17 | + 'port' => false, |
| 18 | + 'tunnel' => false, |
| 19 | + 'address' => false, |
| 20 | + 'type' => CURLPROXY_HTTP, |
| 21 | + 'auth' => array ( |
| 22 | + 'user' => '', |
| 23 | + 'pass' => '', |
| 24 | + 'method' => CURLAUTH_BASIC |
| 25 | + ) |
| 26 | + ); |
| 27 | + |
| 28 | + /** |
| 29 | + * Send a cURL request |
| 30 | + * @param string $url URL to send the request to |
| 31 | + * @param mixed $body request body |
| 32 | + * @param array $headers additional headers to send |
| 33 | + * @param string $username Authentication username (deprecated) |
| 34 | + * @param string $password Authentication password (deprecated) |
| 35 | + * @return SoapClientResponse |
| 36 | + * @throws \Exception if a cURL error occurs |
| 37 | + */ |
| 38 | + public static function send($url, $headers = array(), $body = null, $username = null, $password = null) |
| 39 | + { |
| 40 | + self::$handle = curl_init(); |
| 41 | + |
| 42 | + curl_setopt_array(self::$handle, array( |
| 43 | + CURLOPT_URL => self::encodeUrl($url), |
| 44 | + CURLOPT_POSTFIELDS => $body, |
| 45 | + CURLOPT_HTTPHEADER => self::getFormattedHeaders($headers), |
| 46 | + CURLOPT_CONNECTTIMEOUT => 10, |
| 47 | + CURLOPT_TIMEOUT => 10, |
| 48 | + CURLOPT_RETURNTRANSFER => true, |
| 49 | + CURLOPT_SSL_VERIFYPEER => false, |
| 50 | + CURLOPT_SSL_VERIFYHOST => false, |
| 51 | + CURLOPT_POST => true, |
| 52 | + CURLOPT_VERBOSE => true, |
| 53 | + CURLOPT_HEADER => true |
| 54 | + )); |
| 55 | + if (self::$socketTimeout !== null) { |
| 56 | + curl_setopt(self::$handle, CURLOPT_TIMEOUT, self::$socketTimeout); |
| 57 | + } |
| 58 | + // supporting deprecated http auth method |
| 59 | + if (!empty($username)) { |
| 60 | + curl_setopt_array(self::$handle, array( |
| 61 | + CURLOPT_HTTPAUTH => CURLAUTH_BASIC, |
| 62 | + CURLOPT_USERPWD => $username . ':' . $password |
| 63 | + )); |
| 64 | + } |
| 65 | + if (!empty(self::$auth['user'])) { |
| 66 | + curl_setopt_array(self::$handle, array( |
| 67 | + CURLOPT_HTTPAUTH => self::$auth['method'], |
| 68 | + CURLOPT_USERPWD => self::$auth['user'] . ':' . self::$auth['pass'] |
| 69 | + )); |
| 70 | + } |
| 71 | + if (self::$proxy['address'] !== false) { |
| 72 | + curl_setopt_array(self::$handle, array( |
| 73 | + CURLOPT_PROXYTYPE => self::$proxy['type'], |
| 74 | + CURLOPT_PROXY => self::$proxy['address'], |
| 75 | + CURLOPT_PROXYPORT => self::$proxy['port'], |
| 76 | + CURLOPT_HTTPPROXYTUNNEL => self::$proxy['tunnel'], |
| 77 | + CURLOPT_PROXYAUTH => self::$proxy['auth']['method'], |
| 78 | + CURLOPT_PROXYUSERPWD => self::$proxy['auth']['user'] . ':' . self::$proxy['auth']['pass'] |
| 79 | + )); |
| 80 | + } |
| 81 | + $response = curl_exec(self::$handle); |
| 82 | + $error = curl_error(self::$handle); |
| 83 | + $info = self::getInfo(); |
| 84 | + if ($error) { |
| 85 | + throw new \Exception($error); |
| 86 | + } |
| 87 | + // Split the full response in its headers and body |
| 88 | + $header_size = $info['header_size']; |
| 89 | + $header = substr($response, 0, $header_size); |
| 90 | + $body = substr($response, $header_size); |
| 91 | + |
| 92 | + return new SoapClientResponse($info, $header, $body); |
| 93 | + } |
| 94 | + |
| 95 | + public static function getInfo() |
| 96 | + { |
| 97 | + return curl_getinfo(self::$handle); |
| 98 | + } |
| 99 | + public static function getCurlHandle() |
| 100 | + { |
| 101 | + return self::$handle; |
| 102 | + } |
| 103 | + public static function getFormattedHeaders($headers) |
| 104 | + { |
| 105 | + $formattedHeaders = array(); |
| 106 | + $combinedHeaders = array_change_key_case(array_merge((array) $headers, self::$defaultHeaders)); |
| 107 | + foreach ($combinedHeaders as $key => $val) { |
| 108 | + $formattedHeaders[] = $val; |
| 109 | + } |
| 110 | + if (!array_key_exists('user-agent', $combinedHeaders)) { |
| 111 | + $formattedHeaders[] = 'user-agent: soapclient-request/1.0'; |
| 112 | + } |
| 113 | + if (!array_key_exists('expect', $combinedHeaders)) { |
| 114 | + $formattedHeaders[] = 'expect:'; |
| 115 | + } |
| 116 | + return $formattedHeaders; |
| 117 | + } |
| 118 | + private static function getArrayFromQuerystring($query) |
| 119 | + { |
| 120 | + $query = preg_replace_callback('/(?:^|(?<=&))[^=[]+/', function ($match) { |
| 121 | + return bin2hex(urldecode($match[0])); |
| 122 | + }, $query); |
| 123 | + parse_str($query, $values); |
| 124 | + return array_combine(array_map('hex2bin', array_keys($values)), $values); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Ensure that a URL is encoded and safe to use with cURL |
| 129 | + * @param string $url URL to encode |
| 130 | + * @return string |
| 131 | + */ |
| 132 | + private static function encodeUrl($url) |
| 133 | + { |
| 134 | + $url_parsed = parse_url($url); |
| 135 | + $scheme = $url_parsed['scheme'] . '://'; |
| 136 | + $host = $url_parsed['host']; |
| 137 | + $port = (isset($url_parsed['port']) ? $url_parsed['port'] : null); |
| 138 | + $path = (isset($url_parsed['path']) ? $url_parsed['path'] : null); |
| 139 | + $query = (isset($url_parsed['query']) ? $url_parsed['query'] : null); |
| 140 | + if ($query !== null) { |
| 141 | + $query = '?' . http_build_query(self::getArrayFromQuerystring($query)); |
| 142 | + } |
| 143 | + if ($port && $port[0] !== ':') { |
| 144 | + $port = ':' . $port; |
| 145 | + } |
| 146 | + $result = $scheme . $host . $port . $path . $query; |
| 147 | + return $result; |
| 148 | + } |
| 149 | +} |
0 commit comments