簡體   English   中英

槍口發布給我錯誤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
        );
    }
}

如果刪除過濾器,然后簡單地更改帖子以加入客戶端,則可以看到來自我的用戶控制器的json。 一旦將其更改回發布,就會再次出現錯誤。

路由資源使用存儲方法發布到與索引方法相同的uri。 作為中陳述這里和向下滾動到部分“已處理通過資源控制器操作”。

我最終將主體更改為查詢,它按原樣運行良好,可以同時使用資源類和耗時。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM