You’re getting a 404 because --data-urlencode isn’t part of the URL, it is part of the curl command in the example docs, so you’re sending a request to the wrong URL. Try removing --data-urlencode and it should work.
I’m not a PHP guy so I can’t offer much help, I’ll leave it to others with more PHP experience to offer advice.
All I can see from a brief glance is that I don’t understand why you’re setting the Accept: application/vnd.twitchtv.v3+json header. that’s not needed. The Refresh Access Token docs don’t require setting any additional headers, just the specified querystring params.