I got it fixed, using this function request_filesystem_credentials(), it seems to initialize things and gets the proper credentials.
Found it in an example in this article: An Introduction to the WordPress Filesystem API.
My code:
# get credentials
function connect_fs()
{
global $wp_filesystem;
if( false === ($credentials = request_filesystem_credentials('')) )
{
return false;
}
//check if credentials are correct or not.
if(!WP_Filesystem($credentials))
{
request_filesystem_credentials('');
return false;
}
return true;
}
function donload_images($image_title, $image_url)
{
global $wp_filesystem;
#path to save to
$files_path = ABSPATH . '/image';
$save_file_to = $files_path ."https://wordpress.stackexchange.com/". $image_title;
#check if requested file is on server
$file_exists = file_exists($save_file_to);
if(connect_fs()){
// Get file
if(!$file_exists)
{
#Download file info
$file_to_save = file_get_contents($image_url);
//success
if($wp_filesystem->put_contents($save_file_to, $file_to_save, FS_CHMOD_FILE)){
}
else //Fail
{
}
return $file_to_save;
}
else //File exists
{
}
}
}