Вход Регистрация
Файл: vendor/lusitanian/oauth/src/OAuth/Common/Http/Client/CurlClient.php
Строк: 99
<?php
namespace OAuthCommonHttpClient;

use 
OAuthCommonHttpExceptionTokenResponseException;
use 
OAuthCommonHttpUriUriInterface;

/**
 * Client implementation for cURL
 */
class CurlClient extends AbstractClient
{
    
/**
     *  If true, explicitly sets cURL to use SSL version 3. Use this if cURL
     *  compiles with GnuTLS SSL.
     *
     *  @var bool
     */
    
private $forceSSL3 false;

    
/**
     * @param bool $force
     * @return CurlClient
     */
    
public function setForceSSL3($force)
    {
        
$this->forceSSL3 $force;
        return 
$this;
    }

    
/**
     * Any implementing HTTP providers should send a request to the provided endpoint with the parameters.
     * They should return, in string form, the response body and throw an exception on error.
     *
     * @param UriInterface $endpoint
     * @param mixed $requestBody
     * @param array $extraHeaders
     * @param string $method
     * @return string
     * @throws TokenResponseException
     * @throws InvalidArgumentException
     */
    
public function retrieveResponse(UriInterface $endpoint$requestBody, array $extraHeaders = array(), $method 'POST')
    {
        
// Normalize method name
        
$method strtoupper($method);

        
$this->normalizeHeaders($extraHeaders);

        if( 
$method === 'GET' && !empty($requestBody) ) {
            throw new 
InvalidArgumentException('No body expected for "GET" request.');
        }

        if( !isset(
$extraHeaders['Content-type'] ) && $method === 'POST' && is_array($requestBody) ) {
            
$extraHeaders['Content-type'] = 'Content-type: application/x-www-form-urlencoded';
        }

        
$extraHeaders['Host']       = 'Host: '.$endpoint->getHost();
        
$extraHeaders['Connection'] = 'Connection: close';

        
$ch curl_init();

        
curl_setopt($chCURLOPT_URL$endpoint->getAbsoluteUri());

        if( 
$method === 'POST' || $method === 'PUT' ) {
            if( 
$requestBody && is_array($requestBody) ) {
                
$requestBody http_build_query($requestBodynull'&');
            }

            if( 
$method === 'PUT' ) {
                
curl_setopt($chCURLOPT_CUSTOMREQUEST'PUT');
            } else {
                
curl_setopt($chCURLOPT_POSTtrue);
            }

            
curl_setopt($chCURLOPT_POSTFIELDS$requestBody);
        } else {
            
curl_setopt($chCURLOPT_CUSTOMREQUEST$method);
        }

        if( 
$this->maxRedirects ) {
            
curl_setopt($chCURLOPT_FOLLOWLOCATIONtrue);
            
curl_setopt($chCURLOPT_MAXREDIRS$this->maxRedirects);
        }

        
curl_setopt($chCURLOPT_TIMEOUT$this->timeout);
        
curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
        
curl_setopt($chCURLOPT_HEADERfalse);
        
curl_setopt($chCURLOPT_HTTPHEADER$extraHeaders);

        if( 
$this->forceSSL3 ) {
            
curl_setopt($chCURLOPT_SSLVERSION3);
        }

        
$response     curl_exec($ch);
        
$responseCode curl_getinfo($chCURLINFO_HTTP_CODE);

        if( 
false === $response ) {
            
$errNo  curl_errno($ch);
            
$errStr curl_error($ch);
            
curl_close($ch);
            if (empty(
$errStr)) {
                throw new 
TokenResponseException'Failed to request resource.'$responseCode );
            }
            throw new 
TokenResponseException'cURL Error # '.$errNo.': '.$errStr$responseCode );
        }

        
curl_close($ch);

        return 
$response;
    }
}
Онлайн: 1
Реклама