通过命令加载需要访问令牌的脚本

时间:2014-03-04 14:51:55

标签: php oauth

我使用以下代码使用Instagram API生成访问令牌。

$code = $_GET['code'];
$client_id = '123456789';
$client_secret = '8cf03fedc8bb44f1beccb0ffb529b487';
$redirect_uri = 'http://testsite.com/redirect.php';
$scope = 'relationships';
$url = "https://api.instagram.com/oauth/authorize?client_id=".$client_id."&redirect_uri=".$redirect_uri."&scope=".$scope."&response_type=code";

if(empty($code)) {
    echo '<a href="'.$url.'">Login with Instagram</a>';
}

点击上面的链接后,用户将被重定向到$redirect_uri,但网址中包含代码值。

这通过浏览器对我很好。但是,我似乎无法加载$url并通过命令从URL获取$code。无论如何通过命令行生成访问令牌?

1 个答案:

答案 0 :(得分:1)

PHP可以从命令行接受参数:

调用脚本,如下所示:

php /path/to/script.php code client_id

然后,在PHP脚本中,您可以使用以下命令来访问参数:

<?php

$code = $argv[1];
$client_id = $argv[2];

// TO-DO: Validate the user input
// rest of your script...

请注意,第一个参数$argv[0]始终是用于运行脚本的名称。