簡體   English   中英

Guzzle 不拋出異常

[英]Guzzle not throwing exceptions

我正在圍繞 themoviedb.org api 構建一個 class 包裝器。我正在為請求使用 guzzle 7,但它似乎沒有拋出任何異常。

namespace App\Classes;

use App\Models\Movie;
use App\Models\Series;
use GuzzleHttp\Client;

use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Handler\CurlHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use GuzzleHttp\Psr7\Uri;

use Psr\Http\Message\RequestInterface;

class TMDBScraper{

private string $apiKey;
private string $language;
private Client $client;
private const API_URL = "http://api.themoviedb.org/3/";
private const IMAGE_URL = "http://image.tmdb.org/t/p/";
private const POSTER_PATH_SIZE = "w500";
private const BACKDROP_PATH_SIZE = "original";


public function __construct(string $apiKey = "default_api_key") {
    $this->apiKey = $apiKey;
    $this->language = app()->getLocale();

    $handlerStack = new HandlerStack(new CurlHandler());
    $handlerStack->unshift(Middleware::mapRequest(function (RequestInterface $request) {
        return $request->withUri(Uri::withQueryValues($request->getUri(), [
            'api_key' => $this->apiKey,
            'language' => $this->language
        ]));
    }));

    $this->client = new Client([
        'base_uri' => self::API_URL,
        'handler' => $handlerStack
    ]);

}

public function search($screenplayType, $query): ?array {

    try {

        $response = json_decode($this->client->get('search/' . $screenplayType, [
            'query' => compact('query')
        ])->getBody());

        return $this->toModel($response, $screenplayType);

    } catch (GuzzleException $e) {
        echo $e->getMessage();
        return null;
    }

}

... more code }

我嘗試使用錯誤的 api 鍵,但沒有拋出客戶端異常。 我還嘗試將 http_errors 設置為 true,默認情況下應該設置它,但它也不起作用。

你可以試試這段代碼:

$handler = new CurlHandler();
$stack = HandlerStack::create($handler);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM