Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Undefined array key 0 #10

Open
dietcheese opened this issue Mar 28, 2025 · 0 comments
Open

Undefined array key 0 #10

dietcheese opened this issue Mar 28, 2025 · 0 comments

Comments

@dietcheese
Copy link

dietcheese commented Mar 28, 2025

Having a difficult time getting this working within Laravel.

Always getting a "Undefined array key 0" error. I am authenticating and getting an access token successfully.

Part of my class:

use Sugarcrm\REST\Client\SugarApi;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Log;

class SugarCRMService
{
    protected $client;
    protected $server;
    protected $credentials;

    public function __construct()
    {
        // Retrieve server URL and credentials from config (which pulls from .env)
        $this->server = Config::get('services.sugarcrm.url');
        $this->credentials = [
            'username'  => Config::get('services.sugarcrm.username'),
            'password'  => Config::get('services.sugarcrm.password'),
            'platform'  => Config::get('services.sugarcrm.platform'),
            'client_id' => Config::get('services.sugarcrm.client_id'),
        ];
        
        Log::info('Initializing SugarCRM Service', $this->credentials);
        
        try {
            // Instantiate the SugarApi client using the server URL and credentials.
            $this->client = new SugarApi($this->server, $this->credentials);
            
            // Check if the client is authenticated; if not, log in.
            if (!$this->client->isAuthenticated()) {
                $this->client->login();
            }
            
            // Log the token details (printing only the access_token for brevity)
            $token = $this->client->getAuth()->getToken();
            Log::info('Logged into SugarCRM', [
                'access_token' => $token->access_token,
                'expiration'   => $token->expiration,
            ]);
        } catch (\Exception $e) {
            Log::error('Failed to initialize SugarCRM client', [
                'message' => $e->getMessage(),
                'trace'   => $e->getTraceAsString(),
            ]);
            throw $e;
        }
    }

public function testAccount(string $accountId = 'be46e9f4-27e2-4357-9af4-b18039e1f231'): array
    {
        try {
            // Ensure the client is authenticated.
            if (!$this->client->isAuthenticated()) {
                $this->client->login();
            }
            
            // Build the custom endpoint URL.
            $endpoint = 'custom/account/' . $accountId;
            
            // Call the endpoint using the built-in get() method.
            $response = $this->client->get($endpoint);
            
            Log::info('Received response from testAccount', [
                'endpoint' => $endpoint,
                'response' => $response->toArray(),
            ]);
            
            return $response->toArray();
        } catch (\Exception $e) {
            Log::error('Failed testAccount call', [
                'endpoint' => 'custom/account/' . $accountId,
                'error'    => $e->getMessage(),
                'trace'    => $e->getTraceAsString(),
            ]);
            throw $e;
        }
    }
}
[2025-03-28 21:41:38] local.INFO: Logged into SugarCRM {"access_token":"12e9f4b1-c3bc-49e7-bf5b-f8114b62f8c0","expiration":1743500488} 
[2025-03-28 21:41:38] local.ERROR: Failed testAccount call {"endpoint":"custom/account/be46e9f4-27e2-4357-9af4-b18039e1f231","error":"Undefined array key 0","trace":"#0 
[2025-03-28 21:41:38] local.ERROR: Undefined array key 0 {"exception":"[object] (ErrorException(code: 0): Undefined array key 0 at /shared/httpd/test/htdocs/vendor/michaelj2324/php-rest-client/src/Endpoint/Provider/AbstractMultiVersionEndpointProvider.php:31)
[stacktrace]

It should be noted that when I use Guzzle instead, the same endpoint works fine:

         // Create a new Guzzle client.
            $guzzleClient = new GuzzleClient();
            
            // Make a GET request with the OAuth token set in headers.
            $response = $guzzleClient->request('GET', $url, [
                'headers' => [
                    'OAuth-Token' => $this->client->getAuth()->getToken()->access_token,
                ]
            ]);
            
            // Decode the response JSON.
            $data = json_decode($response->getBody()->getContents(), true);
            
            Log::info('Received response from testAccount (raw request)', [
                'url'  => $url,
                'data' => $data,
            ]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant