如何使用infobip-api(php)向用户发送短信进行验证



我已经从这里尝试了以下代码但是存在一个名为no HttpRequest类的错误。我使用的是php版本5.6.8(xampv3.2.1)。任何人都请帮助我。

<?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 sfdsdf=='  // name&password
));
$request->setBody('{  
   "from":"test",  //from
   "to":"000", // number
   "text":your verification code is = 000."
}');
try {
  $response = $request->send();
  echo $response->getBody();
} catch (HttpException $ex) {
  echo $ex;
}

由于您使用的是php 5.4或更高版本,您的扩展库中似乎没有php_http.dll文件(除非有人能找到我遗漏的文件??)。

在更新php.ini配置文件以包含扩展名后,我在启动Apache服务器时发现的唯一错误。

检查此链接。保存。
然后尝试这个程序:

include_once('HttpRequest.php'); //where HttpRequest.php is the saved file
$url= 'http://www.google.com/';
$r = new HttpRequest($url, "POST");
var_dump($r->send());  

如果它不起作用,那么您可能需要自己编译HTTP包。你可以在这里找到更多。

$mob_no="mobile number";
$msg="Your message";
$sender_id='your sender id eg:VJPOTPYTL';
$str = trim(str_replace(' ', '%20', $msg));
$url="http://dnd.9starabs.com/app/smsapi/index.php?key=55942cc305ef6&type=text&contacts=".$mob_no."&senderid=".$sender_id."&msg=".$str."";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_exec($ch);
curl_close($ch);

解决方案是安装PECL php扩展。关于堆栈溢出,已经给出了好几次答案,比如在这个线程上:

"PHP致命错误:类';HttpRequest';未找到";

您可以使用CURL而不是PECL通过web应用程序发送短信。Infobip的博客对此进行了很好的解释和描述。我建议试试:

http://www.infobip.com/blog/step-by-step-php-tutorial/

最新更新