Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

swamydeshetty's avatar

Im getting the timeout issue while hitting the zoho apis

[2024-11-11 12:27:27] local.ERROR: cURL error 28: Failed to connect to www.zohoapis.com port 443 after 10002 ms: Timeout was reached (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) for https://www.zohoapis.com/crm/v2/Contacts?per_page=200&page=3 {"userId":1,"exception":"[object] (Illuminate\Http\Client\ConnectionException(code: 0): cURL error 28: Failed to connect to www.zohoapis.com port 443 after 10002 ms: Timeout was reached (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) for https://www.zohoapis.com/crm/v2/Contacts?per_page=200&page=3 at C:\xampp\htdocs\sendcrux-main\vendor\laravel\framework\src\Illuminate\Http\Client\PendingRequest.php:855) [stacktrace] im getting this error , i have 14k leads and contacts 1887 i dont know why im geting this issue how can i solve this please help me to solve this issue public function callback(Request $request) { // Step 1: Get the authorization code from the query parameters $code = $request->query('code');

    // Step 2: Retrieve customer ID and client credentials
    $customer_id = $request->user()->customer->id;
    $clientId = env('ZOHO_CLIENT_ID');
    $clientSecret = env('ZOHO_CLIENT_SECRET');
    $redirectUri = env('ZOHO_REDIRECT_URI') ;

    
    
    // Step 3: Get the location (region) and accounts server from the request
    $location = $request->input('location', 'us'); // Default to 'us' if no location is provided
    $accountsUrl = $request->input('accounts-server', 'https://accounts.zoho.com'); // Default to 'https://accounts.zoho.com'
    
    // Define API URL based on the location
    $apiBaseUrls = [
        'us' => 'https://www.zohoapis.com',
        'eu' => 'https://www.zohoapis.eu',
        'in' => 'https://www.zohoapis.in',
        'au' => 'https://www.zohoapis.com.au',
        'cn' => 'https://www.zohoapis.com.cn',
    ];
    
    // Determine the correct API URL based on the location
    $apiBaseUrl = $apiBaseUrls[$location] ?? $apiBaseUrls['us']; // Default to 'us' if no matching location
    
    // dd($accountsUrl);
    // Step 4: Exchange the authorization code for an access token
    $response = Http::asForm()->post("$accountsUrl/oauth/v2/token", [
        'grant_type'    => 'authorization_code',
        'client_id'     => $clientId,
        'client_secret' => $clientSecret,
        'redirect_uri'  => $redirectUri,
        'code'          => $code,
    ]);



    if ($response->successful()) {
        $accessToken = $response->json()['access_token'];
        $refreshToken = $response->json()['refresh_token'];
        
        // Fetch contacts with pagination
        $page = 1;
        $allContacts = [];
        $hasMoreRecords = true;

        while ($hasMoreRecords) {
            $response = Http::withHeaders([
                'Authorization' => 'Zoho-oauthtoken ' . $accessToken,
            ])->get("$apiBaseUrl/crm/v2/Contacts", [
                'per_page' => 200,
                'page'     => $page,
            ]);

            $emaildata = $response->json();

            if (isset($emaildata['data'])) {
                $allContacts = array_merge($allContacts, $emaildata['data']);
                $hasMoreRecords = $emaildata['info']['more_records'];
                $page++;
            } else {
                return redirect('/')->with('error', 'Failed to retrieve contacts from Zoho.');
            }
        }

        // Extract Owner data and save the record as needed
        if (!empty($allContacts)) {
            $ownerData = $allContacts[0]['Owner'];
            $ownerEmail = $ownerData['email'];
            $ownerName = $ownerData['name'];
            $zohoId = $ownerData['id'];
        }

        // Save or update CRM data in the database
        $exists = ZohoCrm::where('email', $ownerEmail)->where('customer_id', $customer_id)->first();

        if (!$exists) {
            $crm = new ZohoCrm();
            $crm->name = $ownerName;
            $crm->email = $ownerEmail;
            $crm->account_id = $zohoId;
            $crm->refreshToken = $refreshToken;
            $crm->region = $accountsUrl;
            $crm->location = $location;
            $crm->customer_id = $customer_id;
            $crm->total_contacts = count($allContacts);
            $crm->save();

            $request->session()->flash('alert-success', trans('Zoho CRM link created.'));
        } else {
            $exists->refreshToken = $refreshToken;
            $exists->total_contacts = count($allContacts);
            $exists->save();
            $request->session()->flash('alert-success', trans('Zoho CRM link updated.'));
        }

        return redirect()->action('ZohoIntegrationController@index');
    } else {
        return redirect('/')->with('error', 'Failed to authenticate with Zoho. Check logs for details.');
    }
}
 my code how to handle this issue
0 likes
0 replies

Please or to participate in this conversation.