首页 > 解决方案 > 在codeigniter中发送短信

问题描述

当我发送短信响应总是在邮递员中显示一个空的 JsonNode

我的代码是

function sendmessage(){
        $mobileNo=$this->input->post('mobile');
        $channel='';
        $pass='';
        $password=base64_encode($pass);
        $message='Hello, This is a test message';
    
        $postData=array([
                  'channel' =>array(
                       'channel' =>  $channel,
                       'password' => $password,
                     ),
                  'messages'=>array(
                        'text'=>$message,
                        'msisdn'=>$mobileNo,
                        "source"=>"littlebloom",
                      )
            ]);
    $dataread=json_encode($postData);
    


/* API URL*/
      $url="https://secure-gw.fasthub.co.tz/fasthub/messaging/json/api";
/* init the resource */
      $ch = curl_init();
      curl_setopt_array($ch, array(
        CURLOPT_URL => $url,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_POST => true,
        CURLOPT_POSTFIELDS => $dataread,
      ));
      /* Ignore SSL certificate verification */
       curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
       curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
      /* get response */
      $output = curl_exec($ch);
      /* Print error if any */
      if(curl_errno($ch))
      {
        $array=array( 'error'=> curl_error($ch));
        echo json_encode($array);
      }
      else{
        $array = array(
          "message"    => $output
        );
      }
      curl_close($ch);
      //MESSAGE CODE END
      echo $output;
}

这是我在 codeigniter 中发送短信的功能。客户端提供 FastHub 短信服务提供的 url,并提供下面给出的 Json 请求代码。我在邮递员中使用它,当我在给定的 url 使用这个 json 作为行时,它给出了成功的 msg,但是当我使用我的 codeigniter 代码时,它显示错误“Empty JsonNode”。我如何在我的代码中定义这个 json。

短信服务提供商是 FastHub

它的json要求就像

"channel":{
       "channel":113474,
       "password":"Base64(sha-256(plain_password))"
  },
"messages":[
   {
      "text":"test message",
      "msisdn":"255754088816",
      "source":"TEST"
   }
]
}

如何在 $postData 中发送这种类型的 json...

标签: phpjsoncodeignitersmssms-gateway

解决方案


尝试将内容类型设置为 JSON 将其添加到 $ch 变量下。在其他 setopts 之后或之前的某个地方

$headers = array( "Content-Type: application/json", ); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);


推荐阅读