2016-10-17 123 views
0

我正在嘗試使用他們的php library將我的應用程序與SendGrid API documentation集成。Sendgrid API與Symfony2集成

我安裝了它使用的作曲家(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

你的代碼和documentation是有關SendGrid PHP SDK的最新版本。您應該安裝最新版本才能使其工作。

composer require sendgrid/sendgrid ~5.1