如何使用 php 从 API 下载用户的推特图片

how to download users twitter images from API with php

我正在尝试下载在我的网站上注册的 Twitter 用户图片,这样我就可以将它们保存在我的文件中,以便稍后插入我的数据库以供我的网站使用。

这是我的推特 API:

 $user_pic = $user_info->profile_image_url_https;

我尝试了这个并且它有效,但我希望它将图像下载到我的基本文件,即 profilePictures/:

$twitterimage = file_get_contents($user_pic);
    $fp = fopen('path_to_filename.png', 'w');
    fwrite($fp, $twitterimage); 
    fclose($fp); 

现在正在下载,但我如何下载 Twitter 图片并将它们保存在我的基本文件中 "profilePictures/"?

我找到了解决方案,我所要做的就是这个

$user_pic= str_replace('_normal','',$user_pic);
    $Imgurl = $user_pic;
    $target_path = "profilePictures/";
    $filename = basename($Imgurl);
    $saveLoc = $target_path . $filename;
    file_put_contents($saveLoc, file_get_contents($Imgurl));

然后将 $saveLoc 变量保存到我的数据库中