简体   繁体   中英

Guzzle post gives me error 500, get works fine

I'm trying to build an API with api key and secret using laravel and guzzle. I am building both the api and the client using laravel.

I have a problem when I try to access a simple controller to get a json with a list of users from the database. It works fine when I'm not using the authentication, it fails when I do beacause I need to change to using post method so that the api gets the secret and the app_id:

GuzzleHttp \ Exception \ ServerException (500)

Server error response [url] http://myapi.api/api/v1/users [status code] 500 [reason phrase] Internal Server Error

On my client:

$_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);

On my 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
    }
});

My controller:

class UsersController extends BaseController {
    public function index()
    {
        $users = User::orderBy('username', 'asc');

        return Response::json(array(
            'error' => false,
            'users' => $users->get()->toArray()),
            200
        );
    }
}

If I remove the filter and simply change post to get on my client, I can see the json that comes from my users controller. As soon as I change it back to post, I get my error again.

Route resource uses the store method to post to the same uri as the index method. As stated within here and scrolling down to the 'Actions Handled By Resource Controller' part.

我最终将主体更改为查询,它按原样运行良好,可以同时使用资源类和耗时。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM