1: <?php
2:
3: namespace SMSApi\Proxy\Http;
4:
5: use SMSApi\Proxy\Proxy;
6:
7: class Curl extends AbstractHttp implements Proxy {
8:
9: private $response = array( 'code' => null, 'output' => null );
10:
11: public function execute( \SMSApi\Api\Action\AbstractAction $action ) {
12:
13: try {
14:
15: $this->uri = $action->uri();
16: $this->file = $action->file();
17:
18: if ( $this->uri == null ) {
19: throw new \SMSApi\Exception\ProxyException( "Invalid URI" );
20: }
21:
22: if ( !empty( $this->file ) && file_exists( $this->file ) ) {
23:
24: $this->toConnect( $this->file );
25: } else {
26:
27: $this->toConnect();
28: }
29:
30: $this->checkCode( $this->response[ 'code' ] );
31:
32:
33: if ( empty( $this->response[ 'output' ] ) ) {
34: throw new \SMSApi\Exception\ProxyException( 'Error fetching remote content empty' );
35: }
36: } catch ( \Exception $ex ) {
37: throw new \SMSApi\Exception\ProxyException( $ex->getMessage() );
38: }
39:
40: return $this->response[ 'output' ];
41: }
42:
43: private function toConnect( $filename = null ) {
44:
45: $body = "";
46:
47: $this->headers[ 'User-Agent' ] = 'SMSApi';
48: $this->headers[ 'Accept' ] = '';
49:
50: if ( $filename ) {
51:
52: $this->headers[ 'Content-Type' ] = 'multipart/form-data; boundary=' . $this->boundary;
53:
54: $body = $this->prepareFileContent( $filename );
55: } else {
56: $this->headers[ 'Content-Type' ] = 'application/x-www-form-urlencoded';
57: }
58:
59: $this->doCurl( $body );
60: }
61:
62: private function doCurl( $body ) {
63:
64: if ( isset( $this->uri ) ) {
65:
66: $url = $this->uri->getSchema() . "://" . $this->uri->getHost() . $this->uri->getPath();
67:
68: $curl = curl_init();
69:
70: curl_setopt( $curl, CURL_HTTP_VERSION_1_1, true );
71:
72: curl_setopt( $curl, CURLOPT_HEADER, false );
73:
74: curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, false );
75:
76: if ( $this->getPort() != 80 ) {
77: curl_setopt( $curl, CURLOPT_PORT, intval( $this->getPort() ) );
78: }
79:
80: if ( isset( $this->timeout ) ) {
81: curl_setopt( $curl, CURLOPT_CONNECTTIMEOUT, $this->timeout );
82: }
83:
84: if ( isset( $this->maxRedirects ) ) {
85: curl_setopt( $curl, CURLOPT_MAXREDIRS, $this->maxRedirects );
86: }
87:
88: if ( !$curl ) {
89: throw new \SMSApi\Exception\ProxyException( 'Unable to connect' );
90: }
91:
92:
93: if ( $this->method == "POST" ) {
94:
95: $body = $this->renderQueryByBody( $this->uri->getQuery(), $body );
96:
97: curl_setopt( $curl, CURLOPT_URL, $url );
98:
99: curl_setopt( $curl, CURLOPT_POST, true );
100:
101: curl_setopt( $curl, CURLOPT_POSTFIELDS, $body );
102: } else {
103: curl_setopt( $curl, CURLOPT_URL, $url . '?' . $this->uri->getQuery() );
104: }
105:
106: $curlHeaders = array( );
107: foreach ( $this->headers as $key => $value ) {
108: $curlHeaders[ ] = $key . ': ' . $value;
109: }
110:
111: curl_setopt( $curl, CURLOPT_HTTPHEADER, $curlHeaders );
112:
113: curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1 );
114:
115: $this->response[ 'output' ] = curl_exec( $curl );
116: $this->response[ 'code' ] = curl_getinfo( $curl, CURLINFO_HTTP_CODE );
117:
118: curl_close( $curl );
119: }
120: }
121:
122: }