【问题标题】:hash_hmac() expects parameter 2 to be string, array givenhash_hmac() 期望参数 2 是字符串,给定数组
【发布时间】:2019-04-04 05:07:25
【问题描述】:

我收到此错误:

[2019-04-04 05:00:04] local.ERROR: hash_hmac() expects parameter 2 to be
 string, array given {"exception":"[object] (ErrorException(code: 0): hash_hmac() 
expects parameter 2 to be string, array given at /home/domains/domain.com/smm/app/Http/Controllers/CoinPaymentsController.php:132)[stacktrace]
#0 [internal function]: Illuminate\\Foundation\\Bootstrap\\HandleExceptions->handleError(2, 'hash_hmac() exp...', '/home/u37281288...', 132, Array)#1 

这是我的代码:

 public function ipn(Request $request)
    {
        if (!$request->filled('ipn_mode') || !$request->filled('merchant')) {
            activity('coinpayments')
                ->withProperties(['ip' => $request->ip()])
                ->log('Missing POST data from callback.');
            die();
        }

        if ($request->input('ipn_mode') == 'httpauth') {
            //Verify that the http authentication checks out with the users supplied information
            if ($request->server('PHP_AUTH_USER') != $this->merchantId || $request->server('PHP_AUTH_PW') != $this->secretKey) {
                activity('coinpayments')
                    ->withProperties(['ip' => $request->ip()])
                    ->log('Unauthorized HTTP Request');
                die();
            }

        } elseif ($request->input('ipn_mode') == 'hmac') {
            // Create the HMAC hash to compare to the recieved one, using the secret key.
// line 132 of the error...
            $hmac = hash_hmac("sha512", $request->all(), $this->secretKey);

            if ($hmac != $request->server('HTTP_HMAC')) {
                activity('coinpayments')
                    ->withProperties(['ip' => $request->ip()])
                    ->log('Unauthorized HMAC Request');
                die();
            }

        } else {
            activity('coinpayments')
                ->withProperties(['ip' => $request->ip()])
                ->log('Unauthorized HMAC Request');
            die();
        }

        // Passed initial security test - now check the status
        $status = intval($request->input('status'));
        $statusText = $request->input('status_text');

        if ($request->input('merchant') != $this->merchantId) {
            activity('coinpayments')
                ->withProperties(['ip' => $request->ip()])
                ->log('Mismatching merchant ID. MerchantID:' . $request->input('merchant'));
            die();
        }

我正在尝试在我的网站上添加 CoinPayments.. 当我设置 IPN URL 时出现此错误.. 付款已进入我的帐户,但无法下载产品..

【问题讨论】:

    标签: php laravel coinpayments-api


    【解决方案1】:

    在将错误信息发布到 stackoverflow 之前,您需要学习正确阅读错误信息, $request->all() 给你一个数组,但 hash_hmac() 方法需要一个字符串,仅此而已, 如果你想要所有数据。然后你可以使用implode() 函数从输入数组中创建一个字符串。

    $inputs = $request->all();
    $string = implode("",$inputs);
    $hmac = hash_hmac("sha512", $string, $this->secretKey); 
    

    【讨论】:

    • 但我需要提供所有数据
    【解决方案2】:

    错误告诉您传递给函数hash_hmac() 的第二个参数是一个数组,它必须是一个字符串。

    $request->all()) 以数组的形式检索所有输入数据

    尝试传递输入名称而不是全部,例如:

    $name = $request->input('name');
    $hmac = hash_hmac("sha512", $name, $this->secretKey); 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-02
      • 1970-01-01
      • 2020-01-27
      • 2023-03-15
      相关资源
      最近更新 更多