我使用Slim框架作爲背景來調用API。如何在php Slim框架中添加參數?
我已經的ConfigEd composer.json
文件:
{
"require": {
"slim/slim": "^2.6",
"slim/middleware": "*"
},
"autoload": {
"psr-4": {"App\\Controller\\": "server/controllers/"}
}
}
而且index.php
文件:
<?php
require 'vendor/autoload.php';
$configs = array( 'mode'=>'development',
'debug'=>true,
'log.enabled' => true,
'templates.path' => './templates',
'cookies.secret_key' => 'secret'
);
$app = new \Slim\Slim($configs);
$app->get('/login', function() use ($app) {
$app->render('login.php');
});
$app->post('/login', function() use ($app) {
$process= new App\Controller\MiddleProcess();
// process request to call API
$process->transferRequest();
});
$app->run();
?>
我在目錄server/controllers/
有一個文件MiddleProcess.php
:
<?php
namespace App\Controller;
class MiddleProcess {
public $url = 'http://api.com/';
function transferRequest() {
$app = \Slim\Slim::getInstance();
$post_datas = $app->request->post();
$token = $app->getCookie('token');
$url = $this->url.'/login';
// How can I add param: `$token` and change 'url', then continue to call API here
// example: http://api.com/login?access_token=$token
}
}
?>
如何改變url
請求nd添加參數access_token
。然後繼續調用API?
如果你想簡單地重定向,我的答案在下面就足夠了。 – r3wt