【问题标题】:Guzzle post gives me error 500, get works fineGuzzle post 给我错误 500,得到正常工作
【发布时间】:2014-10-20 15:42:08
【问题描述】:

我正在尝试使用 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。一旦我将其更改回帖子,我就会再次收到错误。

【问题讨论】:

    标签: php curl laravel-4 guzzle


    【解决方案1】:

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

    【讨论】:

    • 我需要能够利用资源类,我的应用程序将使用这些 url 添加/插入/选择...,我怎样才能做到这一点?
    • 您可以通过单独定义路线来完成这项工作。但我的建议是按照文档中的说明使用资源控制器,因为这会带来更安静的解决方案。
    • 是的,我明白了,那么你建议我如何为 api 客户端进行身份验证?如果不在过滤器中,我应该在哪里做?
    【解决方案2】:

    我最终将 body 更改为 query,它工作正常,可以同时使用资源类和 guzzle。

    【讨论】:

      猜你喜欢
      • 2012-02-19
      • 1970-01-01
      • 2020-05-20
      • 2020-06-27
      • 2018-06-01
      • 2015-01-18
      • 1970-01-01
      • 2017-09-03
      • 2013-06-26
      相关资源
      最近更新 更多