Wordpress PHP - 将图像下载到媒体库并获取thumbnail_id

How would I download image into my wordpress media library and get thumbnail_id, I've tried the below code

$res = $client->request('GET', $url);

$html = ''.$res->getBody();

$crawler = new Crawler($html);

$manga_cover = $crawler->filter('.cover')->filter('img')->attr('src');

$cover_url = 'https://'.$manga_cover;

// image url
print_r($cover_url);