dtjzpg5313 2019-08-11 16:39
浏览 1117
已采纳

array_merge()的PHP错误:预期参数2是一个数组,给定null

I'm trying to make a http post request to my Laravel API from Ionic 4. I've made other Post successfully before with the same API. But for some reason I can't make this one work.

The method on the Laravel backend receives an ID and then sends a email with a PDF attached.

I've test it with Postman tool and it works fine. But when I test on the Ionic 4 App it doesn't work.

I tried changing the headers with different options without luck.

I have set the CORS on Laravel backend like this:

header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers: X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method, Authorization");
header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE");
header("Allow: GET, POST, OPTIONS, PUT, DELETE");
$method = $_SERVER['REQUEST_METHOD'];
if($method == "OPTIONS") {
    die();
}

On Postman it works as expected: https://i.imgur.com/sNuJCzK.png https://i.imgur.com/ZgLJA20.png

This is my Player.service.ts:

    pdf(idPlayer): Observable<any> {

      let json = {
              idplayer : idPlayer
      };

      let params = "json=" + JSON.stringify(json);

       const httpOptions = {
         headers: new HttpHeaders({
            'Accept': 'application/json',
            'Authorization' : localStorage.getItem("token"),
            'Content-Type' : 'application/x-www-form-urlencoded',
         })
       };

       console.log("PDF PARAMS:");
       console.log(params);

      return this._http.post('http://127.0.0.1:8000/api/pdf/email', params, httpOptions);
    }

This is the function that calls the service on my Tab-Profile.page.ts:

    enviarPdf() {
      this._playerService.pdf(this.player._id).subscribe(
        response => {
          console.log("SEND PDF:");
          console.log(response);
        },
        error => {
          console.log("error sendPDF:");
          console.log(error);
        });
    }

This is the error on Chrome console:

Chrome error message

EDIT:

Here is my php function in PlayerController.php:

    public function ToPdf(Request $request) {

      // Obtener usuario identificado
      $token = $request->header('Authorization');
      $jwtAuth = new \JwtAuth();
      $checkToken = $jwtAuth->checkToken($token);

      $user = $jwtAuth->checkToken($token, true);

      $json = $request->input('json', null);
      $params_array = json_decode($json, true);

      $id_player = $params_array['idplayer'];

      $player = Player::find($id_player);

      $pdf = PDF::loadView('templatePdf', $player);

      $pdf_name = $player->id . '.pdf';

      $content = $pdf->output();

      \Storage::disk('players')->put('/pdf/' . $pdf_name, $content);

      $exists = \Storage::disk('player')->exists('/pdf/' . $pdf_name);

      if (!$exists) {
        $data = array(
          'code' => 400,
          'status' => 'error',
          'message' => 'Error PDF.',
          'pdf' => $pdf_name
        );
      }
      else {
        $email_params = array(
          'userName' => $user->name,
          'playerId' => $player->id,
          'playerName' => $player->name,
          'playerSurname' => $player->surname,
        );

        // Mail::to($user->email)->queue(new EmailPdf($email_params));
        Mail::to($user->email)->queue(new EmailPdf($email_params));

        \Storage::disk('players')->delete('/pdf/' . $pdf_name);

        $data = array(
          'code' => 200,
          'status' => 'success',
          'message' => 'Email sended successfully.',
        );
      }

      return response()->json($data, $data['code']);

    }
  • 写回答

1条回答 默认 最新

  • doudou0612 2019-08-11 19:13
    关注

    The second parameter for PDF::loadView() must be an array.

    Try to change this

    $pdf = PDF::loadView('templatePdf', $player);
    

    into

    $pdf = PDF::loadView('templatePdf', ['player'=>$player]);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 数学的三元一次方程求解
  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题