【问题标题】:How to send sms using infobip api in php?如何在 php 中使用 infobip api 发送短信?
【发布时间】:2023-03-27 11:38:01
【问题描述】:

我在使用 Infobip 短信网关时遇到问题。当我执行此代码时,我不知道下面的代码有一个名为 no HttpRequest class found 的错误。任何人请帮助我。

<?php

$request = new HttpRequest();
$request->setUrl('https://api.infobip.com/sms/1/text/single');
$request->setMethod(HTTP_METH_POST);

$request->setHeaders(array(
  'accept' => 'application/json',
  'content-type' => 'application/json',
  'authorization' => 'Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=='
));

$request->setBody('{  
   "from":"InfoSMS",
   "to":"41793026727",
   "text":"Test SMS."
}');

try {
  $response = $request->send();

  echo $response->getBody();
} catch (HttpException $ex) {
  echo $ex;
}

【问题讨论】:

  • 您可以删除 javascript 和 xml 标签。能否请您提供您的环境信息、php 版本、pecl http 版本以及您收到的确切错误消息(包括调用信息)?
  • 请告诉我如何执行此操作

标签: php json infobip-api infobip


【解决方案1】:

你可以在下面使用..它对我有用。

    $curl = curl_init();

    curl_setopt_array($curl, array(
        CURLOPT_URL => 'https://lzz25w.api.infobip.com/sms/2/text/advanced',
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => '',
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 0,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => 'POST',
        CURLOPT_POSTFIELDS =>'{"messages":[{"from":"$from","destinations":[{"to":"'.$to.'"}],"text":"'.$message.'"}]}',
        CURLOPT_HTTPHEADER => array(
            'Authorization: {authorization}',
            'Content-Type: application/json',
            'Accept: application/json'
        ),
    ));

    $response = curl_exec($curl);

    curl_close($curl);
   // echo $response;

【讨论】:

    猜你喜欢
    • 2016-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多