title: How to GET remote JSON or XML API data from within PHP and assign a return object as PHP variable?
body: <p><strong>What I'm doing:</strong></p>
  
  <p>I'm writing a custom program in PHP which pulls data via API from an online LMS service. Right now, I'm trying to implement the available single-sign-on functionality.</p>
  
  <p>This part of the program needs to execute a GET request to the API when a button is clicked (via js or php POST or ?) and ultimately redirect the users browser to a URL which is supplied in the response from the API.</p>
  
  <p>The API allows the choice of an XML or JSON response and I would prefer to use JSON but will make do with XML if needed.</p>
  
  <p><strong>From the API documentation on making requests:</strong></p>
  
  <blockquote>
    <p>All requests listed in this document should contain a content-type
    (XML or JSON) in the request header and be prefixed with the following
    base Uri: <a href="https://api.example.com/v1.svc" rel="nofollow">https://api.example.com/v1.svc</a></p>
    
    <p>E.g. The Uri to GET a list of Users in XML format would be:</p>
    
    <p>Content-Type: text/xml</p>
    
    <p>GET <a href="https://api.example.com/v1.svc/users?apikey=MY-KEY&amp;source=MY-APP" rel="nofollow">https://api.example.com/v1.svc/users?apikey=MY-KEY&amp;source=MY-APP</a></p>
  </blockquote>
  
  <p><strong>Below is what I'm trying to implement:</strong></p>
  
  <blockquote>
    <p>How to get the a user's LoginKey</p>
    
    <p>Once you have the user id that you want to sign on you need to make a
    GET request to /users/{user-id} which will return information about
    the user. Included in this is a LoginKey which you can use to redirect
    the user's browser to. </p>
    
    <p>eg.</p>
    
    <p>GET
    <a href="https://api.example.com/v1.svc/users/USER-ID?apikey=YOUR_API_KEY&amp;source=sampleapp" rel="nofollow">https://api.example.com/v1.svc/users/USER-ID?apikey=YOUR_API_KEY&amp;source=sampleapp</a></p>
    
    <p>Response from API: </p>
  
  <pre><code>&lt;User&gt;
    &lt;Id&gt;abc12345678&lt;/Id&gt;
    &lt;UserName&gt;rich_demo@example.com&lt;/UserName&gt;
    &lt;FirstName&gt;Rich&lt;/FirstName&gt;
    &lt;LastName&gt;Chetwynd&lt;/LastName&gt;
    .....
    &lt;LoginKey&gt;https://demo.example.com/login.aspx?loginkey=xxxzzzyyy777222&lt;/LoginKey&gt;
  &lt;/User&gt;
  </code></pre>
  </blockquote>
  
  <p>The <code>&lt;LoginKey&gt;</code> object data is the URL which I need to ultimately redirect the user's browser to.</p>
  
  <p>I am new to working with APIs and have tried a ton of methods which I could not get to work before posting. If you know how to accomplish this I would be very grateful if you shared your knowledge.</p>
  
  <p>Thanks.</p>