接收简单JSON请求时,RequestException.php中的Laravel ServerException

时间:2016-05-09 12:18:32

标签: php json laravel curl laravel-5

我使用Laravel 5框架与制造商沟通,而且我在收到回调方面苦苦挣扎:

以下是路线(将它们设置为两个,只是为了测试):

Route::post('/callback', 'PrintController@callback');
Route::get('/callback', 'PrintController@callback');

控制器中的简单方法:

public function callback(Request $request)
{
    var_dump( $request );
    //Storage::put('request.txt', $request);
}

它工作正常,如果我手动打开网站(意思是,它转储请求并稍后创建请求文件),但是当这样调用时:

/**
 * Test callback option
 */
public function testCallback()
{
    $callback_url = 'http://project.dev/callback';

    // Prepare response
    $response = array(
        'time' => -microtime(true),
    );

    $data = [
        "id" => 907,
        "current_state" => "Shipped",
        "merchant_sku" => "BST123",
        "ordered_on_date" => "2015-08-16T00:00:00+0200",
        "ship_by_date" => "2015-08-21T00:00:00+0200",
        "shipping_carrier" => "USPS",
        "shipping_tracking" => "9499907123456123456781",
    ];

    try {
        $result = Guzzle::post($callback_url, [
            'verify' => false,
            'headers' => [
                'Content-Type' => 'application/json',
            ],
            'json' => $data,
        ]);
        $body = json_decode($result->getBody(), true);

        // Fill response
        if ($body['success'] == true) {
            $response['success'] = true;
            $response['order_id'] = $body['work_order_id'];
        } else {
            $response['success'] = false;
        }
    } catch (Exception $e) {
        // Set error
        $response['success'] = false;
        $response['message'] = $e->getMessage();
    }

    // Save execution time
    $response['time'] += microtime(true);
    $response['time'] = round(abs($response['time']), 4);

    return $response;
}

我收到此错误消息:

ServerException in RequestException.php line 107:
Server error: `POST http://project.dev/callback` resulted in a `500 Internal Server Error` response:
<!DOCTYPE html>
<html>
<head>
<meta name="robots" content="noindex,nofollow" />
<style>
(truncated...)

编辑:我在php_error.log中收到的唯一错误消息是:

[09-May-2016 12:24:20 UTC] PHP Deprecated:  Automatically populating $HTTP_RAW_POST_DATA is deprecated and will be removed in a future version. To avoid this warning set 'always_populate_raw_post_data' to '-1' in php.ini and use the php://input stream instead. in Unknown on line 0

任何想法我做错了什么和/或如何调试和解决这个问题?

顺便说一下:通信在保存机器中的两个laravel框架之间发生,这意味着它site.devproject.dev发送请求。

编辑:在接收POST时在laravel错误日志中找到此信息。它适用于GET。

[2016-05-09 15:21:25] local.ERROR: exception 'Illuminate\Session\TokenMismatchException' in D:\project.dev\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\VerifyCsrfToken.php:67

为什么Laravel甚至关心这一点,因为我发送'verify' => false请求?

1 个答案:

答案 0 :(得分:0)

我解决了。我将路径移到中间件之外,因此不需要令牌验证:

Route::post('/callback', 'PrintController@callback');
Route::get('/callback', 'PrintController@callback');

Route::group(['middleware' => ['web']], function () {
    ...
}

以防其他人遇到同样的问题:此处需要中间件,因为制造商不使用相同的会话而无法提供所需的令牌。