title: PHP : generate a token using a Post method
body: <p>Good day,</p>
  
  <p>Before getting back on Stackoverflow, I have been googling the entire afternoon without being really successful. </p>
  
  <p>What I am trying to do is to get a token from myfox api by referring to their <a href="https://api.myfox.me/dev/authentication" rel="nofollow noreferrer">doc</a> which says </p>
  
  <blockquote>
    <p>A fresh token must be generated to be able to perform API calls. The
    token can be requested by calling the following method
    <a href="https://api.myfox.me/oauth2/token" rel="nofollow noreferrer">https://api.myfox.me/oauth2/token</a> and providing the parameters below
    (through POST): client_id, client_secret, username, password and
    grant_type set to password.</p>
  </blockquote>
  
  <p>Hence my code :</p>
  
  <pre><code>function getToken()
  {
  $clientID = "a65000ee0c57f2e37260e90c375c3";
  $clientSecret = "MyLongSecretCode";
  $exportFile = "myfile.txt";
  $userName = "somebody@somewhere.com";
  $userPass = "myPassword123";
  $sourceWebsite = "https://api.myfox.me/oauth2/token?client_id=" . $clientID .  "&amp;client_secret=" . $clientSecret . "&amp;username=" . $userName . "&amp;password=" . $userPass . "&amp;grant_type=password";
  
  file_put_contents($exportFile, fopen($sourceWebsite , 'r'));
  }
  </code></pre>
  
  <p>All I'm getting is a PHP error which says that the method is not allowed. </p>
  
  <p>Any idea what I am missing here?</p>
  
  <p>Many thanks for your kind help on this subject.</p>
  
  <p><strong>Edit 17.03.2017 :</strong></p>
  
  <p>I have been told by other users that I might be able to achieve this by using curl and it looks like, again, by reading the documentation that this is something that I can do :</p>
  
  <blockquote>
    <p>A fresh token must be generated to be able to perform API calls. The
    token can be requested by calling the following method
    <a href="https://api.myfox.me/oauth2/token" rel="nofollow noreferrer">https://api.myfox.me/oauth2/token</a> and providing the parameters below
    (through POST): client_id, client_secret, username, password and
    grant_type set to password.  curl -u CLIENT_ID:CLIENT_SECRET
    <a href="https://api.myfox.me/oauth2/token" rel="nofollow noreferrer">https://api.myfox.me/oauth2/token</a> -d
    'grant_type=password&amp;username=YOUR_USERNAME&amp;password=YOUR_PASSWORD' 
    or curl <a href="https://api.myfox.me/oauth2/token" rel="nofollow noreferrer">https://api.myfox.me/oauth2/token</a> -d
    'grant_type=password&amp;client_id=CLIENT_ID&amp;client_secret=CLIENT_SECRET&amp;username=YOUR_USERNAME&amp;password=YOUR_PASSWORD'</p>
  </blockquote>
  
  <p>Now, for my question : is there a way to translate this curl -u query into a php instruction and to output the contents to a file out of it that would look like :</p>
  
  <pre><code>{"access_token":"********************************","expires_in":3600,"token_type":"Bearer","scope":null,"refresh_token":"********************************"}
  </code></pre>
  
  <p>Thanks again for your help.</p>