0

我的控制器

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class MessageController extends Controller
{
    public function manyMessage(Request $request){

        $message = $request->input("message");
        $mobile = $request->input("mobile");
        $encodeMessage = urlencode($message);
        $authKey = 'authKey';
        $senderId = 'senderId';
        $route = 4;
        $postData = $request->all();
        $mobileNumber = implode('',$postData['mobile']);
        $arr = str_split($mobileNumber,11);
        $mobiles = implode(",",$arr);
        $data = array(
            'authKey' => $authKey,
            'mobiles' => $mobiles,
            'message' => $encodeMessage,
            'sender' => $senderId,
            'route' => $route,

        );
        $url = "https://api.infobip.com";

        $ch = curl_init();
        curl_setopt_array($ch, array(
           CURLOPT_URL => $url,
           CURLOPT_RETURNTRANSFER =>true,
           CURLOPT_POST => true,
           CURLOPT_POSTFIELDS => $postData,
        ));

       curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0);
       curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0);

       $output = curl_exec($ch);

       if(curl_errno($ch)){

           echo 'error :'. curl_error($ch);

       }

       curl_close($ch);
        Session::flash('success', 'Message sent Successfully!');
       return redirect("/records");
    }
}

看法

<form role="form" action="{{url("/sendMessage")}}" method="post">
                                {{csrf_field()}}
                                <textarea type="hidden" class="form-control" row="4">Write Text</textarea><br>
                                <input type="submit" class="pull-left btn btn-primary btn-small" value="Send"/><br /><br />

                                <input type="checkbox" id="select_all" name="select_all"/> Select All<br/><br />
                                <div class="row">
                                    <div class="col-lg-12">
                                        <div class="panel panel-default">
                                            <div class="panel-heading">
                                                New Converts / First Timer
                                            </div>
                                            <!-- /.panel-heading -->
                                            <div class="panel-body">
                                                <div class="dataTable_wrapper">
                                                    <table class="table table-striped table-bordered table-hover" id="dataTables-example">
                                                        <thead>
                                                        <tr>
                                                            <th></th>
                                                            <th>Name</th>
                                                            <th>Phone Number</th>
                                                            <th>Home Address</th>
                                                            <th>Email Address</th>
                                                            <th>Date</th>
                                                            <th>Action</th>
                                                        </tr>
                                                        </thead>
                                                        <tbody>
                                                        @if(isset($record))
                                                            @foreach($record as $record)

                                                                <tr class="odd gradeX">
                                                                    <td><input type="checkbox" class="checkbox" name="mobile[]" value="{{$record->phone}}"></td>
                                                                    <td>{{$record->first_name}} - {{$record->last_name}}</td>
                                                                    <td>{{$record->phone}}</td>
                                                                    <td>{{$record->home_address}}</td>
                                                                    <td>{{$record->email}}</td>
                                                                    <td>{{ Carbon\Carbon::parse($record->created_at)->format('d-m-Y H:i:s') }}</td>
                                                                    <td><div class="">
                                                                            <a href="{{URL::to('/editNewConvert')}}/{{$record->id}}" class="btn btn-primary btn-xs">Edit</a>
                                                                            <a href="{{URL::to('/deleteNewConvert')}}/{{$record->id}}" class="btn btn-danger btn-xs">Delete</a>
                                                                        </div></td>
                                                                </tr>
                                                            @endforeach
                                                        @endif
                                                        </tbody>
                                                    </table>
                                                </div>
</form>

错误

(1/1) 在 MessageController.php 中的 ErrorException 数组到字符串转换(第 37 行)在 HandleExceptions->handleError(8, 'Array to string conversion', 'C:\xampp\htdocs\blog\app\Http\Controllers\MessageController .php', 37, array('request' => object(Request), 'message' => null, 'mobile' => array('08028479963'), 'encodeMessage' => '', 'authKey' => 'authKey', 'senderId' => 'senderId', 'route' => 4, 'postData' => array('_token' => 'p7Cg8f0nQzCjwuuVz83WmQngvp7zeiGnQRQS8FPi', 'dataTables-example_length' => '10', 'mobile' => '数组'), 'mobileNumber' => '08028479963', 'arr' => 数组('08028479963'), 'mobiles' => '08028479963', 'data' => array('authKey' => 'authKey', 'mobiles' => '08028479963', 'message' => '', 'sender' => 'senderId', '路由' => 4), 'url' => ' https://api.infobip.com', 'ch' => 资源)) 在 curl_setopt_array(re

4

2 回答 2

1

您已创建$data的参数数组并尝试将$postData作为 post 字段发送,请将$postData 更改为 $data 并重

更改 CURL 代码如下:

    $ch = curl_init();
    curl_setopt_array($ch, array(
       CURLOPT_URL => $url,
       CURLOPT_RETURNTRANSFER =>true,
       CURLOPT_POST => true,
       CURLOPT_POSTFIELDS => $postData,
    ));

   curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0);
   curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0);

   $output = curl_exec($ch);

   if(curl_errno($ch)){
       echo 'error :'. curl_error($ch);
   }
   curl_close($ch);
于 2017-09-01T18:42:23.140 回答
0

使用Laravel SMS API 插件,只需根据您的 SMS 服务添加 2-3 个配置参数即可。其他一切都由插件处理。

于 2018-05-01T23:04:00.453 回答