首页 > 解决方案 > guzzle 未定义函数 getStatusCode()

问题描述

我的代码如下:

require_once $DOCUMENT_ROOT.'/autoload.php';

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;

...

if($token!='' && $email!='')
{
    $client = new \GuzzleHttp\Client();

    $response = $client->request('POST', 'https://graph.microsoft.com/v1.0/me/sendmail', [
        'headers' => [
            'Authorization' => 'Bearer ' . $token,
            'Content-Type' => 'application/json;odata.metadata=minimal;odata.streaming=true'
        ],
        'body' => $email
    ]);
    if($response.getStatusCode() === 201) {
        exit('<h1>Email sent, check your inbox</h1>');
    } else {
        exit('<h2>There was an error sending the email.</h2> Status code: ' . $response.getStatusCode());
    }
}

我不断收到错误Call to undefined function getStatusCode()。我知道我需要添加另一个用途,但我已经尝试了所有方法,但无法让它工作,每次都出现同样的错误。

标签: phpguzzle

解决方案


解决它。从微软复制代码,它应该是:

if($response->getStatusCode() === 202)

成功也是 202 而不是 201


推荐阅读