Sendgrid API与Symfony2集成

时间:2016-10-17 12:58:14

标签: symfony sendgrid

我尝试使用他们的documentation将我的应用与SendGrid API php library集成。

我使用composer(composer.json)

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

创建了一个服务(services.yml):

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

服务:

<?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();
    }
}

我收到以下错误:

Notice: Undefined property: SendGrid::$client

我无法让它发挥作用,也不知道接下来该做什么。

提前致谢

1 个答案:

答案 0 :(得分:1)

您的代码和documentation与最新版本的SendGrid PHP SDK相关。您应该安装最新版本以使其正常工作。

composer require sendgrid/sendgrid ~5.1