我想通过我的php页面发送短信。
我的短信网关提供商给了我这样的api " http://sms.xyz.com/uname=abc&pass=xyz&phone=1234567890&msg=hello" (在浏览器中访问此URL会将短信发送到电话号码)
我希望我的脚本只是访问此URL(以便发送短信)并返回到我自己的页面(无论结果如何)并打印"发送消息"
请建议任何方法。
答案 0 :(得分:0)
使用 cURL
发送消息并获取回复,并使用 header()
重定向用户。
<?php
function send($phone, $msg)
{
$API_URL = "http://sms.xyz.com/";
$UNAME = "abc";
$PASS = "xyz";
$query = array(
'uname'=>$UNAME,
'pass'=>$PASS,
'phone'=>$phone,
'msg'=>$msg
);
$url = $API_URL . "?" . http_build_query($query);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url); // set the url to fetch
curl_setopt($ch, CURLOPT_HEADER, 0); // set headers (0 = no headers in result)
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // type of transfer (1 = to string)
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_TIMEOUT_MS, 4000); // time to wait in miliseconds
$content = curl_exec($ch); // Make the call
curl_close($ch);
echo "Response Message == ".$content; // Response message
}
// Now hit the function
send('9220022002', 'Hello world!');
// and redirect wherever you want
header("location:http://www.google.com");
// or
header("location:/something/anything");
// or
header("location:index.php");
exit(); // always use exit after using header('Location')