     1: <?php
     2:
     3: namespace SMSApi\Proxy\Http;
     4:
     5: use SMSApi\Exception\ProxyException;
     6: use SMSApi\Proxy\Proxy;
     7:
     8: class Curl extends AbstractHttp implements Proxy
     9: {
    10:     protected function makeRequest($url, $query, $file)
    11:     {
    12:         $body = $this->prepareRequestBody($file);
    13:
    14:         $headers = $this->prepareRequestHeaders($file);
    15:
    16:         $curl = curl_init();
    17:
    18:         curl_setopt( $curl, CURL_HTTP_VERSION_1_1, true );
    19:
    20:         curl_setopt( $curl, CURLOPT_HEADER, false );
    21:
    22:         curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, false );
    23:
    24:         if ( $this->getPort() != 80 ) {
    25:             curl_setopt( $curl, CURLOPT_PORT, intval( $this->getPort() ) );
    26:         }
    27:
    28:         if ( isset( $this->timeout ) ) {
    29:             curl_setopt( $curl, CURLOPT_CONNECTTIMEOUT, $this->timeout );
    30:         }
    31:
    32:         if ( isset( $this->maxRedirects ) ) {
    33:             curl_setopt( $curl, CURLOPT_MAXREDIRS, $this->maxRedirects );
    34:         }
    35:
    36:         if ( !$curl ) {
    37:             throw new ProxyException( 'Unable to connect' );
    38:         }
    39:
    40:         if ( $this->method == "POST" ) {
    41:
    42:             $body = $this->renderQueryByBody($query, $body);
    43:
    44:             curl_setopt( $curl, CURLOPT_URL, $url );
    45:
    46:             curl_setopt( $curl, CURLOPT_POST, true );
    47:
    48:             curl_setopt( $curl, CURLOPT_POSTFIELDS, $body );
    49:         } else {
    50:             curl_setopt( $curl, CURLOPT_URL, $url . '?' . $query);
    51:         }
    52:
    53:         $curlHeaders = array( );
    54:         foreach ( $headers as $key => $value ) {
    55:             $curlHeaders[ ] = $key . ': ' . $value;
    56:         }
    57:
    58:         curl_setopt( $curl, CURLOPT_HTTPHEADER, $curlHeaders );
    59:
    60:         curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1 );
    61:
    62:         $response = array(
    63:             'output' => curl_exec( $curl ),
    64:             'code' => curl_getinfo( $curl, CURLINFO_HTTP_CODE )
    65:         );
    66:
    67:         curl_close( $curl );
    68:
    69:         return $response;
    70:     }
    71: }
    72: