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 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100