This part of your code results in "output already sent" errors/warnings:
Code:
echo $url;
echo $data;
Side note: there is no $data set there, so why try to echo it?