我想通过我的php页面发送短信。
我的短信网关提供商给了我的 API 像
"http://sms.xyz.com/uname=abc&pass=xyz&phone=1234567890&msg=hello"
(在浏览器中访问此 URL 会向电话号码发送短信)
我希望我的脚本只访问此 URL(以便发送短信)并返回我自己的页面(无论结果如何)并打印"消息已发送"
请提出任何方法。
利用cURL
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://sms.xyz.com/uname=abc&pass=xyz&phone=1234567890&msg=hello");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // For HTTPS
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // For HTTPS
curl_exec($ch);
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); // Returns 200 if everything went well
if($statusCode==200)
{
echo "<script>alert('SMS Sent');</script>";
echo "<script>document.location.href='welcome.html'</script>"; //Redirecting back after successfully sent SMS
}
else
{
echo "<script>alert('SMS Sending Failed.');</script>";
}
curl_close($ch);
猜你在JavaScript中寻找"location"。
http://www.w3schools.com/js/js_window_location.asp
您无法重定向回您的页面,除非 API 明确允许它并且具有类似 returnURL 变量的内容。
看看 PHP 中的 cURL。它会将请求发送到其他网页,而不会在浏览器中显示该页面。这样,您就可以将用户保留在您的页面上,而无需重定向他们。