【问题标题】:Curl request not working in laravel 7 but working inside the command line卷曲请求在 laravel 7 中不起作用,但在命令行中起作用
【发布时间】:2021-02-24 05:54:56
【问题描述】:

我用 laravel 7 作为后端制作了一个应用程序,一切都很酷,除了当我尝试使用 firebase 发送通知时,我似乎无法让 curl 工作。

我尝试了this 代码,但 curl 似乎不起作用,如果我直接从命令行尝试服务器上 php 上的代码,代码就可以工作。

我的php版本是7.4.12,系统是centos 8

前端:

<form method="POST" action="{{route('bulksend')}}">
  {{ csrf_field()}}
  <label>Title</label>
  <input type="text" hint="Title" name="title">
  <br>
  <label>Body</label>
  <input type="text" hint="Body" name="body">
  <br>
  <label>Image URL</label>
  <input type="text" hint="Image URL" name="img">
  <br>
  <label>ID</label>
  <input type="text" hint="Image URL" name="id">
  <br>
  <input type="submit"/>
</form>

控制器方法:

public function bulksend(Request $req)
{
    $url = 'https://fcm.googleapis.com/fcm/send';
    $dataArr = array('click_action' => 'FLUTTER_NOTIFICATION_CLICK', 'id' => $req->id, 'status' => "done");
    $notification = array('title' => $req->title, 'text' => $req->body, 'image' => $req->img, 'sound' => 'default', 'badge' => '1',);
    $arrayToSend = array('to' => "my device_token", 'notification' => $notification, 'data' => $dataArr, 'priority' => 'high');
    $fields = json_encode($arrayToSend);
    $headers = array(
        'Authorization: key=' . "my auth",
        'Content-Type: application/json'
    );

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);

    $result = curl_exec($ch);

    curl_close($ch);
    print_r($result);
    return $result;
}

输出只是一个空页面。 在控制台中它打印成功。 感谢您的帮助。

【问题讨论】:

  • 由于某种未知原因,代码在两天后工作

标签: php laravel flutter laravel-7 php-curl


【解决方案1】:

您需要安装php-curl 扩展。
任何 curl php 请求的响应都是 0false

在不工作的服务器中添加这样的扩展:

sudo yum install -y  php-curl

然后重启你的网络服务器

sudo systemctl restart httpd.service

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-10
    • 1970-01-01
    • 2017-11-29
    • 1970-01-01
    • 2016-04-16
    • 1970-01-01
    • 1970-01-01
    • 2014-04-01
    相关资源
    最近更新 更多