【问题标题】:SSL error in CURL call to Twitter API对 Twitter API 的 CURL 调用中的 SSL 错误
【发布时间】:2014-06-14 14:50:21
【问题描述】:

下面是我的控制器的一些代码(别担心,de 键是假的)。我正在使用 ZendService\Twitter\Twitter 模块。几乎一切正常,只是最后一个错误有点奇怪,我想不通:

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 上运行,也不会运行。

谢谢

【问题讨论】:

标签: php curl twitter zend-framework2


【解决方案1】:

从不将验证主机或对等验证设置为 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);
}

【讨论】:

  • 设置上面的结果完全一样
  • 你能检查一下HTTP客户端中是否设置了选项吗?只需一些 var_dump() 语句以及 Twitter 服务如何传递选项。那里可能有问题。通常,在 HTTP 客户端中设置此选项即可。
  • @Dirkos 你暴露了消费者的秘密。立即更改它
  • 都是假数据别担心 :)
【解决方案2】:

我遇到了同样的问题,并在 Google 上找到了这个问题。我知道我应该禁用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();

【讨论】:

    猜你喜欢
    • 2018-08-22
    • 1970-01-01
    • 2013-02-03
    • 2018-09-06
    • 2018-02-19
    • 2018-04-27
    • 2013-09-21
    • 2016-04-27
    • 2021-11-26
    相关资源
    最近更新 更多