PHP Experts - Need your help - Calling Elgg Webservices for external authentication

By bujji1

I am doing external authentication to my elgg site, I mean user sign into PHP based website we have and it internal calls elgg Rest webservices and creates a session. To implement this, I am calling the below URLS (giving example based on GET method )

The above giving me the token and passing the token to below URL<token from the above URL>

and then I am going to

If I hit the three URLs manually on the browser , I mean run the first URL get value , form the second URL based on it and hit it on browser and get the Success XML file and the hit my elgg site . It signs in and takes me to dashboard, no issues. But this is done manually. So to automate this

I am using the below PHP code

  $output = file_get_contents("
  $output = file_get_contents("$token");


But the above PHP takes me to the elgg login page and asks me for credentials. In first two steps authentication happend, so in the thrid step it should take me directly to dashboard, but that is not happening .

The token I am passing is correct. Even I printed the token and tried it manually and it worked. But to automate this using the above simple 3 lines PHP program it fails.

Any ideas what I am doing wrong ?
Thanks a lot for your help