zendframework / ZendService_Twitter

PHP OOP wrapper for the Twitter API
BSD 3-Clause "New" or "Revised" License
22 stars 23 forks source link

HTTP client options not honoured when calling getAccessToken #21

Open SteveTalbot opened 10 years ago

SteveTalbot commented 10 years ago

When a Twitter object is first instantiated, an HTTP client is created, using the http_client_options specified in the array passed into the constructor. So for example, if you want to use curl as your HTTP adapter, you can do something like this:

$config = array(
    'oauth_options' => array(
        'consumerKey' => 'twitter-consumer-key-here',
        'consumerSecret' => 'twitter-consumer-secret-here',
    ),
    'http_client_options' => array(
        'adapter' => 'Zend\Http\Client\Adapter\Curl',
        'curloptions' => array(
             CURLOPT_CAINFO => 'path-to-ca-cert-here',
        )
    ),
);
$twitter = new Twitter($config);

When getAccessToken() is called, the HTTP client is replaced and the http_client_options are not honoured. This means the default stream adapter will be used instead of curl.

The problem seems to be on line 219 of ZendService\Twitter\Twitter.php, but I've been trying for ages and can't find an elegant solution:

$this->setHttpClient($return->getHttpClient($this->options));
giulioprinaricotti commented 10 years ago

The same problem arises also with getRequestToken. I guess the problem it that getAccessToken/getRequestToken are forwarded to the Consumer which isn't forwarded any http_client_options/http_client.

Even setting http_client manually is ignored.

The issue is blocking on my project as the class Socket fails because sslverifypeer = true and I'm not able to disable it.

giulioprinaricotti commented 10 years ago

I ended up setting the httpClient globally for OAuth using OAuth::setHttpClient()

$config = array(
                   'adapter'   => 'Zend\Http\Client\Adapter\Curl',
                    'curloptions' => array(
                            CURLOPT_SSL_VERIFYHOST => false,
                            CURLOPT_SSL_VERIFYPEER => false
                    ),
                 );
$httpClient = new HttpClient(null,$config);
OAuth::setHttpClient($httpClient);

The same $httpClient is given to Twitter() as http_client option

Does anyone have a more elegant solution?

SteveTalbot commented 10 years ago

@giulioprinaricotti Looks like a neat approach

giulioprinaricotti commented 10 years ago

@SteveTalbot thank you. Only drawback I see is that you have to repeat it every time it is needed and setting something global is always a source of problems.

weierophinney commented 4 years ago

This repository has been closed and moved to laminas/laminas-twitter; a new issue has been opened at https://github.com/laminas/laminas-twitter/issues/2.