2015-11-27 14 views
8

Tôi đang sử dụng ứng dụng Symfony2 trên heroku và tôi không thể kết nối với API của mình.cURL timeout trên heroku

tôi giải thích cho bạn những kịch bản:

  1. tôi kết nối với tôi trong ứng dụng của mình thông qua một hình thức
  2. Khi tôi đăng nhập một cách chính xác trong ứng dụng của tôi, một người biết lắng nghe sự kiện được kích hoạt
  3. tôi truy cập vào URL cung cấp cho tôi mã thông báo API và tôi lưu các thông tin đăng nhập đó trong phiên người dùng
  4. Tôi chuyển hướng người dùng được kết nối (đến ứng dụng và API) đến trang chủ của ứng dụng

Vì vậy, vấn đề của tôi là ở điểm 3. Tôi đã thử với cURL và một file_get_contents để truy cập vào URL và cả hai đều không hoạt động.

Khi tôi cố gắng truy cập URL cung cấp mã thông báo OAuth cần thiết để truy cập API của tôi, nó không thành công. Tôi đã cố gắng truy cập URL này theo cách thủ công trong trình duyệt (hoặc Postman) và nó cũng trả về mã thông báo OAuth.

Các event listener:

 // 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); 
     } 

Các lỗi từ Heroku và các bản ghi của tôi:

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 [email protected] 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> [] [] 

Nhờ sự giúp đỡ của bạn!

+1

Bạn đã cố gắng đặt 'CURLOPT_TIMEOUT' chưa? Ví dụ: 'curl_setopt ($ curl, CURLOPT_TIMEOUT, 60);' – scoolnico

+0

Cảm ơn bạn đã trả lời nhưng có, tôi đã thử và nó không hoạt động –

+0

và 'USERAGENT' thì sao? 'curl_setopt ($ curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; U; Linux x86_64; en-US) AppleWebKit/537.36 (KHTML, như Gecko) Chrome/41.0.2225.0 Safari/537.36'); ' – scoolnico

Trả lời

3

Tôi tìm thấy một giải pháp:

EventListener

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

Đây chỉ là một giải pháp để đăng nhập vào API của tôi, whithout sử dụng cURL và URL mà cung cấp cho các token.