candiani
candiani

Reputation: 39

Sendgrid API integration with Symfony2

I'm trying to integrate my app with SendGrid API documentation using their php library.

I installed it using composer (composer.json)

"require": {
    ...
    "sendgrid/sendgrid": "2.0.5",
    "sendgrid/php-http-client": "3.4"
}

Created a service (services.yml):

    sendgrid_service:
    class: AppBundle\Services\SendGridApiIntegration
    arguments: ['%sendgrid_api_key%', '%sendgrid_api_user%']

Service:

<?php

namespace AppBundle\Services;

use SendGrid;

class SendGridApiIntegration
{
    private $api_key;
    private $api_user;

    public function __construct($api_user, $api_key)
    {
        $this->api_user = $api_user;
        $this->api_key = $api_key;
    }


    public function testSendGrid()
    {
//        \SendGrid::register_autoloader();
//        $sg = new \SendGrid($this->api_user, $this->api_key);
        $sg = new \SendGrid($this->api_key);
        $request_body = json_decode('{
          "name": "pet",
          "type": "text"
        }');
        $response = $sg->client->contactdb()->custom_fields()->post($request_body);
        echo $response->statusCode();
        echo $response->body();
        echo $response->headers();
    }
}

I get the following error:

Notice: Undefined property: SendGrid::$client

I can't get it working and don't know what to do next.

Thanks in advance

Upvotes: 0

Views: 336

Answers (1)

Mikhail Prosalov
Mikhail Prosalov

Reputation: 4345

Your code and the documentation is relevant to the latest version of SendGrid PHP SDK. You should install the latest version to make it work.

composer require sendgrid/sendgrid ~5.1

Upvotes: 1

Related Questions