2014-06-14 68 views
0

下面是我的控制器的一些代碼(別擔心,鍵是假的)。我使用ZendService \ Twitter \ Twitter模塊。幾乎一切工作只有最後一個錯誤是有點奇怪,我無法弄清楚:CURL致電Twitter API的SSL錯誤

Unable to enable crypto on TCP connection api.twitter.com: make sure the "sslcafile" or "sslcapath" option are properly set for the environment.

正如你可以看到我下面我的控制器的代碼,你可以看到,無論是驗證點的和主機設置爲false。該適配器已被設置爲Curl而不是HTTP。

<?php 

namespace Twitter\Controller; 

use QDCore\Controller\AbstractController; 
use Zend\Mvc\MvcEvent; 
use Zend\View\Model\JsonModel; 
use ZendService\Twitter\Twitter; 

class GetController extends AbstractController 
{ 

    protected $instance; 

    public function onDispatch(MvcEvent $e) 
    { 
     $config = array(
      'access_token' => array(
       'token' => '1024003resagsDQGyVC5YZ23423PpBNOwefS', 
       'secret' => 'oES8Jergewagewahsh2hTqrYGDJo', 
      ), 
      'oauth_options' => array(
       'consumerKey' => 'QY360Nersehr234gg4aV2pw', 
       'consumerSecret' => 'eEfgdagewa0Hkt4z6nCqHPY1M4wwuubY', 
      ), 
      'username' => 'myusername', 
      'http_client_options' => array(
       'adapter' => 'Zend\Http\Client\Adapter\Curl', 
       'curloptions' => array(
        CURLOPT_SSL_VERIFYHOST => false, 
        CURLOPT_SSL_VERIFYPEER => false, 
       ), 
      ), 
     ); 
     $this->instance = new Twitter($config); 
     return parent::onDispatch($e); 
    } 


    public function indexAction() 
    { 
     $result = new JsonModel(array('message' => 'No valid function call made')); 
     return $result; 
    } 

    public function usertimelineAction() 
    { 
     $options = array(
      'user_id' => 'myaccountname', 
      'count' => 30, 
     ); 
     $twitter = new Twitter($options); 
     $response = $twitter->statuses->userTimeline(); 
     var_dump($response); 
     die; 
     return new JsonModel($response); 
    } 
} 

希望有人對如何解決它有一個想法。我的主域名不在SSL上運行,也不會。

感謝

+0

相關http://stackoverflow.com/questions/16298282/php-and-ssl-ca-verification-os-independent – Xerkus

回答

1

NEVER組驗證主機或對等認證爲false,除非你知道自己在做什麼!

您必須將curl指向您的認證包。對於Linux(基於Debian的系統),即etc/ssl/certs。你可以設置爲「sslcapath」變量:

'http_client_options' => array(
    'adapter' => 'Zend\Http\Client\Adapter\Curl', 
    'curloptions' => array(
     'sslcapath' => '/etc/ssl/certs', 
    ), 
), 

由於道路系統之間變化,這是件好事,它在你的config/autoload/global.php文件中設置選項,用戶可以用一個local.php配置改變。在你的配置:

'http_client' => array(
    'options' => array(
     'sslcapath' => '/etc/ssl/certs', 
    ), 
), 

那麼你的代碼變成:

public function onDispatch(MvcEvent $e) 
{ 
    $app = $e->getApplication(); 
    $sm = $app->getServiceManager(); 
    $cnf = $sm->get('Config'); 

    $config = array(
     'access_token' => array(
      'token' => '1024003resagsDQGyVC5YZ23423PpBNOwefS', 
      'secret' => 'oES8Jergewagewahsh2hTqrYGDJo', 
     ), 
     'oauth_options' => array(
      'consumerKey' => 'QY360Nersehr234gg4aV2pw', 
      'consumerSecret' => 'eEfgdagewa0Hkt4z6nCqHPY1M4wwuubY', 
     ), 
     'username' => 'myusername', 
     'http_client_options' => array(
      'adapter' => 'Zend\Http\Client\Adapter\Curl', 
      'curloptions' => $cnf['http_client']['options'], 
     ), 
    ); 
    $this->instance = new Twitter($config); 
    return parent::onDispatch($e); 
} 
+0

上面設置有確切相同的結果 – Dirkos

+0

你能否檢查這些選項是否在HTTP客戶端中設置?只需要一些var_dump()語句以及Twitter服務如何傳遞選項。那裏可能有問題。通常,在HTTP客戶端中設置此選項正常。 –

+0

這裏你去:https://gist.github.com/anonymous/0d4ca2c73f22c79da33a – Dirkos

0

我有同樣的確切問題,發現這個谷歌。我知道我應該禁用CURLOPT_SSL_VERIFYHOSTCURLOPT_SSL_VERIFYPEER或指定correct path to the local certificates,但不知道該怎麼做。

This answer已經幫了我很多:

$config = array(
    'callbackUrl' => 'http://example.com/callback.php', 
    'siteUrl' => 'http://twitter.com/oauth', 
    'consumerKey' => 'myConsumerKey', 
    'consumerSecret' => 'myConsumerSecret' 
); 
$consumer = new ZendOAuth\Consumer($config); 

// this is the key: 
$adapter = new \Zend\Http\Client\Adapter\Curl(); 
$adapter = $adapter->setCurlOption(CURLOPT_SSL_VERIFYHOST, false); 
$adapter = $adapter->setCurlOption(CURLOPT_SSL_VERIFYPEER, false); 
$httpClient = $consumer->getHttpClient(); 
$httpClient->setAdapter($adapter); 

// now finally fetch a request token 
$token = $consumer->getRequestToken();