Guzzle帖子给了我错误500,得到的工作很好


Guzzle post gives me error 500, get works fine

我正在尝试使用laravel和guzzle构建一个具有API密钥和机密的API。我正在使用laravel构建api和客户端。

当我试图访问一个简单的控制器来从数据库中获取一个带有用户列表的json时,我遇到了一个问题。当我不使用身份验证时,它工作得很好,当我使用时,它失败了,因为我需要改为使用post方法,以便api获得机密和app_id:

GuzzleHttp ' Exception ' ServerException (500)
Server error response [url] http://myapi.api/api/v1/users [status code] 500 [reason phrase] Internal Server Error

关于我的客户:

$_app_id = 'APP001';
$_app_key = '28e336ac6c9423d946ba02d19c6a2632';
$_api_url = 'http://myapi.api/api/v1/users';
$enc_request = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $_app_key, json_encode($request_params), MCRYPT_MODE_ECB));
$params = array();
$params['enc_request'] = $enc_request;
$params['app_id'] = $_app_id;
$client = new GuzzleHttp'Client();
$result = $client->post($_api_url, array(
    'body' =>  $params
));
$res=$result->json();  
var_dump($res);

在我的API上:

Route::group(array('prefix' => 'api/v1'), function(){
     Route::resource('users', 'UsersController');
});
Route::filter('my.filter', function()
{
    $applications = array(
        'APP001' => '28e336ac6c9423d946ba02d19c6a2632', //randomly generated app key 
    );
    try {
        $enc_request = $_REQUEST['enc_request'];
        $app_id = $_REQUEST['app_id'];
        if( !isset($applications[$app_id]) ) {
            throw new Exception('Application does not exist!');
        }
        $params = json_decode(trim(mcrypt_decrypt( MCRYPT_RIJNDAEL_256, $applications[$app_id], base64_decode($enc_request), MCRYPT_MODE_ECB )));
        if( $params == false ){
            throw new Exception('Request is not valid');
            $result['success'] = false;
        }else{
            $result['success'] = true;
        }
    } catch( Exception $e ) {
        $result = array();
        $result['success'] = false;
        $result['errormsg'] = $e->getMessage();
    }
    if($result['success']==false){
        return Response::make('Unauthorized', 401);
        //I have tested and the APP never gets inside here, authentication is correct
    }
});

我的控制器:

class UsersController extends BaseController {
    public function index()
    {
        $users = User::orderBy('username', 'asc');
        return Response::json(array(
            'error' => false,
            'users' => $users->get()->toArray()),
            200
        );
    }
}

如果我删除过滤器并简单地更改post以访问我的客户端,我可以看到来自用户控制器的json。当我把它改回帖子时,我又犯了错误。

Route资源使用store方法发布到与index方法相同的uri。如本文所述,并向下滚动到"资源控制器处理的操作"部分。

我最终更改了主体以进行查询,它运行得很好,可以同时使用资源类和guzzle。

相关文章: