Guzzle向Google API发送HTTP请求,服务器500错误

时间:2017-05-24 02:40:06

标签: php gmail-api guzzle

我使用Guzzle向Gmail API发送HTTP请求。

此部分导致服务器500错误,为什么会这样?

$data = new stdClass; 
$data-> 'topicName' ='projects/sample.com:sample/topics/topic';   
$data-> 'labelIds' = ["INBOX"];
$data-> 'labelFilterAction' = 'include'; 

完整代码:

    require_once __DIR__.'/vendor/autoload.php';
    $client = new Google_Client();
    $client->setScopes("https://www.googleapis.com/auth/gmail.readonly");
    putenv('GOOGLE_APPLICATION_CREDENTIALS=sample.json');
    $client->useApplicationDefaultCredentials();
    // returns a Guzzle HTTP Client
    $httpClient = $client->authorize();
    $data = new stdClass; 
    $data->'topicName' ='projects/sample.com:sample/topics/topic';   
    $data-> 'labelIds' = ["INBOX"];
    $data-> 'labelFilterAction' = 'include';
    $request = new GuzzleHttp\Psr7\Request('POST', 'https://www.googleapis.com/gmail/v1/users/post@sample.com/watch',['Content-type'=>'application/json'],$data);
    $response = $httpClient->send($request);

    var_dump($response);

1 个答案:

答案 0 :(得分:0)

你不应该在$ data object的属性周围加上单引号。

应该是:

$data->topicName ='projects/sample.com:sample/topics/topic';   
$data->labelIds = ["INBOX"];
$data->labelFilterAction = 'include';