heroku上的cURL超时

Max*_*ard 8 php curl heroku symfony

我在heroku上使用Symfony2应用程序,但无法连接到我的API.

我向你解释一下这个场景:

  1. 我通过表单在我的应用程序中连接我
  2. 当我正确登录我的应用程序时,会触发一个事件监听器
  3. 我访问了向我提供API令牌的URL,并将这些凭据保存在用户会话中
  4. 我将连接(到应用程序和API)用户重定向到应用程序的主页

所以我的问题是在第3点.我尝试使用cURL和file_get_contents来访问URL,但两者都没有用.

当我尝试访问向我提供访问API所需的OAuth令牌的URL时,它会失败.我尝试在浏览器(或邮递员)中手动访问此URL,并返回OAuth令牌.

事件监听器:

        // Call of the url to deliver the token
        $clientAPI = $this->em->getRepository('BgAPIBundle:Client')->findOneBy(array('providerAccess' => 'pcv'));
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, urldecode($this->router->generate('fos_oauth_server_token', array('grant_type' => 'http://www.myapp/api/grants/api_key', 'client_id' => $clientAPI->getId() . '_' . $clientAPI->getRandomId(), 'client_secret' => $clientAPI->getSecret(), 'api_key' => $event->getAuthenticationToken()->getUser()->getApiKey()), true)));
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_COOKIESESSION, true);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        $response = curl_exec($curl);
        $this->error = $response; // Only if there is problem
        curl_close($curl);

        // Write in logs
        $date = new \DateTime('now');
        if (preg_match('#error#', $this->error)) {
            $this->logger->error('Error on authentication success to deliver the OAuth token for ' . $event->getAuthenticationToken()->getUser() . ' the ' . $date->format('j/m/Y - H:i:s') . ' with message : ' . $this->error);
        }
Run Code Online (Sandbox Code Playgroud)

来自Heroku和我的日志的错误:

heroku/router:  at=error code=H12 desc="Request timeout" method=GET path="/app_dev.php/oauth/v2/token?grant_type=http://www.app/api/grants/api_key&client_id=261_1ws6mvgmbeisgcsw4wokwk8k400og88gs0csssg0gk0884080s&client_secret=1ghmf01c1a4kc448ssgwg8sw04840c4ww8k00gg4o0k8w04g4&api_key=7f5284ac5ec8b35527d3c16dafa52a89" host=app-max.herokuapp.com request_id=dc8960fd-d154-4e5d-bc2f-34d4f25b8070 fwd="46.51.146.244" dyno=web.1 connect=0ms service=30006ms status=503 bytes=0 
app.ERROR: Une erreur est survenue lors de l'attribution du token OAuth de maxime@tetst.fr le 25/11/2015 - 16:12:40 avec le message : <!DOCTYPE html>     <html>     <head>       <meta name="viewport" content="width=device-width, initial-scale=1">       <style type="text/css">         html, body, iframe { margin: 0; padding: 0; height: 100%; }         iframe { display: block; width: 100%; border: none; }       </style>     <title>Application Error</title>     </head>     <body>       <iframe src="//s3.amazonaws.com/heroku_pages/error.html">         <p>Application Error</p>       </iframe>     </body>     </html> [] []
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助 !

Max*_*ard 3

我找到了一个解决方案:

事件监听器

$tokenController = new FOS\OAuthServerBundle\Controller\TokenController($this->oauth2); //  $this->oauth2 is the @fos_oauth_server.server service
$credentials = $tokenController->tokenAction($request);
$response = $credentials->getContent();
Run Code Online (Sandbox Code Playgroud)

这只是登录 API 的另一种解决方案,无需使用 cURL 和提供令牌的 URL。