如何通过php中为oauth形成的url获取代码

I am using this link:

https://msdn.microsoft.com/en-us/library/dn632721.aspx

I need it to run without user interaction so when I have the following in index.php

$goToUrl = $authorizationUrlBase . '?' . http_build_query($queryParams);

what do I do with this to get the code in php? I have checked for testing purpose using

header("Location:" .$goToUrl);

that it has code in the url formed but if I use above line of code in php, it gets stuck at the new window.

How do I deal with this? I do I fetch that code value without having to redirecting anywhere.

**I need it to run without user interaction since it needs to be run as a cron job. I am using Bing ads api.