Search code examples
phphtmltwitter-bootstraptwitter-oauth

how to download users twitter images from API with php


i am trying to download twitter users images that sign up in my website such that i can save them in my file to insert into my database later on for use in my website.

this is my TWITTER API:

 $user_pic = $user_info->profile_image_url_https;

i tried this and it works but i want it to download the image to my base file which is profilePictures/:

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

now it is being downloading but how can i download the twitter images and save them in my base file "profilePictures/"?


Solution

  • i found the solution, all i had to do is this

    $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));
    

    and then save the $saveLoc variable into my database