1: <?php
2:
3: namespace SMSApi\Proxy\Http;
4:
5: use SMSApi\Proxy\Proxy;
6:
7: class Native extends AbstractHttp implements Proxy
8: {
9: 10: 11:
12: const CONNECT_FOPEN = 1;
13:
14: 15: 16:
17: const CONNECT_SOCKET = 2;
18:
19: protected function makeRequest($url, $query, $file)
20: {
21: $body = $this->prepareRequestBody($file);
22:
23: $headers = $this->prepareRequestHeaders($file);
24:
25: if (!empty($body)) {
26: $url .= '?' . $query;
27: }
28:
29: $headersString = $this->preparePlainTextHeaders($headers);
30:
31: $options = array(
32: 'http' => array(
33: 'method' => $this->method,
34: 'header' => $headersString,
35: 'content' => empty($body) ? $query : $body,
36: )
37: );
38:
39: $context = stream_context_create($options);
40:
41: $fp = fopen($url, 'r', false, $context);
42:
43: $response['code'] = $this->getStatusCode(stream_get_meta_data($fp));
44: $response['output'] = stream_get_contents($fp);
45:
46: if ($fp) {
47: fclose($fp);
48: }
49:
50: return $response;
51: }
52:
53: private function getStatusCode($metaData)
54: {
55: $statusCode = null;
56:
57: if ( isset( $metaData[ 'wrapper_data' ] ) AND is_array( $metaData[ 'wrapper_data' ] ) ) {
58: if (isset($metaData['wrapper_data']['headers']) and is_array($metaData['wrapper_data']['headers'])) {
59: $headers = $metaData['wrapper_data']['headers'];
60: } else {
61: $headers = $metaData['wrapper_data'];
62: }
63:
64: foreach ($headers as $wrapperRow) {
65: if (preg_match( '/^[\s]*HTTP\/1\.[01]\s([\d]+)\sOK[\s]*$/i', $wrapperRow, $code)) {
66: $statusCode = next($code);
67: }
68: }
69: }
70:
71: return $statusCode;
72: }
73:
74: 75: 76: 77:
78: private function preparePlainTextHeaders($headers)
79: {
80: $headersString = "";
81:
82: foreach ($headers as $k => $v) {
83: if (is_string($k)) {
84: $v = ucfirst($k) . ": " . $v;
85: }
86:
87: $headersString .= $v . "\r\n";
88: }
89:
90: return $headersString;
91: }
92: }
93: