Issue with deviantart authentication (PHP)

I am trying to connect to deviantart via their oauth2 mechanism. I do get the code to obtain an access token but then I get some strange 404 error when trying to get the access token via file_get_contents. However, when I copy the url I am able to get the access token as desired. Has this sth. do with header settings? This is the code to get the access token: