HTTP客户端/“ file_get_contents()”由于加载时间而使Laravel应用程序超时

时间:2019-05-24 16:17:48

标签: php laravel

我目前正在使用Guzzle从资源中获取数据,而这在内部 Laravel应用程序。提取API的功能位于控制器的功能中。每当我加载页面(在这种情况下,是在下面的控制器中声明的lead_tracker页面)使用Guzzle请求API时,服务器就会超时并且什么也没有给我。为什么会这样?

以下是相关文件:控制器(web.php)

 <?php

  Route::get('/facebook_for_business', 'PageController@facebook_for_business_page')->middleware('usersession');
  Route::get('/search_dominance', 'PageController@search_dominance_page')->middleware('usersession');
  Route::get('/phone_management', 'PageController@phone_management_page')->middleware('usersession');
  Route::get('/email_management', 'PageController@email_management_page')->middleware('usersession');
  Route::get('/document_center', 'PageController@document_center_page')->middleware('usersession');
  Route::get('/google_adwords', 'PageController@google_adwords_page')->middleware('usersession');
  Route::get('/media_gallery', 'PageController@media_gallery_page')->middleware('usersession');
  Route::get('/lead_tracker', 'PageController@lead_tracker_page')->middleware('usersession');
  Route::get('/push_network', 'PageController@push_network_page')->middleware('usersession');
  Route::get('/sub_account', 'PageController@sub_account_page')->middleware('usersession');
  Route::get('/dashboard', 'PageController@dashboard_page')->middleware('usersession');
  // Route::get('/dashboard', 'PageController@dashboard_page');
  Route::get('/calendar', 'PageController@calendar_page')->middleware('usersession');
  Route::get('/register', 'PageController@register_page');//->middleware('usersession');
  // Route::get('/register', 'PageController@register_page');
  Route::get('/reviews', 'PageController@review_page')->middleware('usersession');
  Route::get('/notes', 'PageController@notes_page')->middleware('usersession');
  Route::get('/events', 'PageController@events_page')->middleware('usersession');

  Route::get('/privelege', 'PageController@privelege_page')->middleware('usersession');
  Route::get('/login', 'PageController@login_page');
  // Route::get('/login', 'PageController@login_page')->middleware('usersession');
  Route::get('/', 'PageController@index');
  Route::get('/logout', 'UsersController@logout');
  Route::get('/get_all_leads', 'LeadTrackerController@get_all');
  Route::get('/get_lead_details', 'LeadTrackerController@show');
  Route::get('/get_admin_id', 'UsersController@get_admin_id');
  Route::get('/get_sub_accounts/{id} ', 'UsersController@get_sub_accounts');


  Route::post('/authenticate_user', 'UsersController@authenticate');
  Route::post('/submit_register', 'UsersController@store');
  Route::post('/submit_add_lead', 'LeadTrackerController@store');
  Route::post('/submit_edit_lead', 'LeadTrackerController@update');
  Route::post('/submit_delete_lead/{id}', 'LeadTrackerController@destroy');

  Route::post('/submit_add_privelege', 'PrivelegesController@store');

相关控制器(PageController.php)

    <?php

    namespace App\Http\Controllers;

    use Illuminate\Http\Request;
    use Illuminate\Support\Facades\URL;

    use Illuminate\Support\Facades\DB;
    use Auth;
    use File;

    class PageController extends Controller {
        private $data = array('title' => '', 'script' => '');

        public function index() {
            return redirect('/login'); 
        }

        public function dataTable($properties = array()){

            /*
                ID label
                columns
                API URL
                column names
            */

            $client = new \GuzzleHttp\Client();

            $idLabel = !isset($properties['id']) ? "a".md5(microtime()) : htmlspecialchars($properties['id']);
            $url = !isset($properties['url']) ? "" : htmlspecialchars($properties['url']);
            $html = "";

            $fetch = $client->request('GET', url($properties['url']), ['connect_timeout' => 3.14]);

            //$html = '<script type="text/javascript">$(document).ready(function() {  $("#'.$idLabel.'").DataTable( {   ';
            //$html .= "ajax: {url: '".$url."',dataSrc: 'data'},columns: [{ data: 'id' }]";
            //$html .= '} ); });</script>';


            $table = "<table id='".$idLabel."'>";
            $table .= "<thead class='thead-dark'><tr><th>id</th></tr></thead>";
            $table .= "</table>";


            $htmls = [
                'div' => $table, 
                'html' => $fetch->getBody()
            ];   

            return $htmls;
        }



        public function lead_tracker_page() {
            $data = $this->data;
            $data['title'] = 'ISR | Lead Tracker';


            $data['dataTable'] = $this->dataTable(['url' => '/get_all_leads']);



            $data['page_script'] = url('/') . '/js/pages/lead_tracker.js';
            return view('pages.lead_tracker_page')->with($data);
        }

任何帮助将不胜感激。谢谢

1 个答案:

答案 0 :(得分:0)

此处将连接超时设置为3.14秒。

    $fetch = $client->request('GET', url($properties['url']), ['connect_timeout' => 3.14]);

这意味着耗时客户端等待3.14秒并尝试连接到请求的URL。如果无法连接到3.14中的URL,则会引发超时错误。

所以您可以做的是:

1)删除超时选项,然后将其设置为默认=>零(0)

(0)=>表示无限期等待(默认行为)

或2)将超时时间提高到至少10秒。

您可以在guzzle official website

了解有关此选项的信息