当我有API链接时,如何通过PHP发送短信

时间:2017-11-20 11:36:07

标签: php sms

我有来自via的链接,我可以发送短信。当我将它放在地址栏中并填写所需的get参数并按回车键时,它可以正常工作。但是如何在控制器动作的中间加载它(如果重要,框架是Yii2)?我尝试使用mail()但无法达到任何结果。 链接如下:

http://sms.***********.com/httpApi/Send.aspx?phone=359.........&body=message&username=xxx&password=xxx

我可以用普通的PHP制作它还是我必须通过javascript?提前谢谢!

2 个答案:

答案 0 :(得分:2)

cURL 允许跨多种协议传输数据,是一个非常强大的系统。它被广泛用作跨网站发送数据的方式,包括API交互和oAuth等。从基本的HTTP请求到更复杂的FTP上传或与身份验证封装的HTTPS站点的交互,cURL的功能不受限制。我们将关注发送GET和POST请求与处理返回的响应之间的简单区别,以及突出显示一些有用的参数。

$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => 'YOUR API URL',
    CURLOPT_USERAGENT => 'cURL Request'
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);

cURL函数背后的基本思想是使用curl_init()初始化cURL会话,然后可以通过curl_setopt()设置所有传输选项,然后可以使用curl_exec()执行会话然后使用curl_close()完成会话。

答案 1 :(得分:1)

你应该使用сURL。

$query = http_build_query([
 'phone' => '359.........',
 'body' => 'message',
 'username' => 'xxx',
 'password' => 'xxx'
]);

$c = curl_init();
curl_setopt($c , CURLOPT_URL , 'http://sms.***********.com/httpApi/Send.aspx/?' . $query);
curl_exec($c); 
curl_close($c);