duanmo5724 2017-03-16 17:37
浏览 82
已采纳

PHP:使用Post方法生成令牌

Good day,

Before getting back on Stackoverflow, I have been googling the entire afternoon without being really successful.

What I am trying to do is to get a token from myfox api by referring to their doc which says

A fresh token must be generated to be able to perform API calls. The token can be requested by calling the following method https://api.myfox.me/oauth2/token and providing the parameters below (through POST): client_id, client_secret, username, password and grant_type set to password.

Hence my 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 .  "&client_secret=" . $clientSecret . "&username=" . $userName . "&password=" . $userPass . "&grant_type=password";

file_put_contents($exportFile, fopen($sourceWebsite , 'r'));
}

All I'm getting is a PHP error which says that the method is not allowed.

Any idea what I am missing here?

Many thanks for your kind help on this subject.

Edit 17.03.2017 :

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 :

A fresh token must be generated to be able to perform API calls. The token can be requested by calling the following method https://api.myfox.me/oauth2/token 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 https://api.myfox.me/oauth2/token -d 'grant_type=password&username=YOUR_USERNAME&password=YOUR_PASSWORD' or curl https://api.myfox.me/oauth2/token -d 'grant_type=password&client_id=CLIENT_ID&client_secret=CLIENT_SECRET&username=YOUR_USERNAME&password=YOUR_PASSWORD'

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 :

{"access_token":"********************************","expires_in":3600,"token_type":"Bearer","scope":null,"refresh_token":"********************************"}

Thanks again for your help.

  • 写回答

1条回答 默认 最新

  • douya1855 2017-03-17 14:38
    关注

    Thanks all a lot for your hints, here is a script that is working :

    I hope that this script (and this post) can at some point be helpful to someone else. I wish you a very nice week-end.


    <?php
    
    $clientID = 'b85036758c385c3cd0c57f2e37260f91';
    $clientSecret = 'MyLongSecretCode';
    $username = 'myemailaddress@provider.net';
    $passwd = 'mypassword';
    
    // Get cURL resource
    $curl = curl_init();
    // Set some options - we are passing in a useragent too here
    curl_setopt_array($curl, array(
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_URL => 'https://api.myfox.me/oauth2/token',
        CURLOPT_USERAGENT => 'Codular Sample cURL Request',
        CURLOPT_POST => 1,
        CURLOPT_POSTFIELDS => array(
            'grant_type' => 'password',
            'client_id' => $clientID,
            'client_secret' => $clientSecret,
            'username' => $username,
            'password' => $passwd
        )
    ));
    // Send the request & save response to $resp
    $resp = curl_exec($curl);   
    
    echo $resp;
    
    // Close request to clear up some resources
    curl_close($curl);
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗