我想在我的laravel应用程序上集成一个sms api

时间:2017-08-30 11:57:43

标签: php laravel-5.4

我的控制器
 

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)ErrorException数组转换为字符串   MessageController.php(第37行)在HandleExceptions-&gt; handleError(8,   &#39;数组到字符串转换&#39;,   &#39; C:\ XAMPP \ htdocs中\博客\应用\ HTTP \控制器\ MessageController.php&#39 ;,   37,数组(&#39;请求&#39; =&gt;对象(请求),&#39;消息&#39; =&gt; null,&#39; mobile&#39; =&gt;   数组(&#39; 08028479963&#39;),&#39; encodeMessage&#39; =&GT; &#39;&#39;,&#39; authKey&#39; =&GT; &#39; AUTHKEY&#39 ;,   &#39; senderId&#39; =&GT; &#39; senderId&#39;,&#39; route&#39; =&GT; 4,&#39; postData&#39; =&GT;阵列(&#39; _token&#39;   =&GT; &#39; p7Cg8f0nQzCjwuuVz83WmQngvp7zeiGnQRQS8FPi&#39;,&#39; dataTables-example_length&#39; =&GT; &#39; 10&#39;,&#39; mobile&#39; =&GT; &#39;阵列&#39),   &#39;移动电话号码&#39; =&GT; &#39; 08028479963&#39;,&#39; arr&#39; =&GT;阵列(&#39; 08028479963&#39),   &#39;移动台&#39; =&GT; &#39; 08028479963&#39;,&#39;数据&#39; =&GT;数组(&#39; authKey&#39; =&gt;&#39; authKey&#39;,   &#39;移动台&#39; =&GT; &#39; 08028479963&#39;,&#39; message&#39; =&GT; &#39;&#39;,&#39;发件人&#39; =&GT; &#39; senderId&#39 ;,   &#39;路线&#39; =&GT; 4),&#39; url&#39; =&GT; &#39; https://api.infobip.com&#39;,&#39; ch&#39; =&GT;资源))   在curl_setopt_array(重新

2 个答案:

答案 0 :(得分:0)

您已创建参数数组 $ data 并尝试将 $ postData 作为帖子字段发送,请将$ 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);

答案 1 :(得分:0)

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