简体   繁体   English

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

[英]How to send sms via php when i have the API link

I have a link from the via which I can send sms. 我有一个来自via的链接,我可以发送短信。 It works when I put it in the address bar and fill the required get params and press enter. 当我把它放在地址栏并填写所需的get参数并按回车键时,它可以正常工作。 But how can I load it in the middle of controller action (the framework is Yii2 if that matters) ? 但是如何在控制器动作的中间加载它(如果重要,框架是Yii2)? I tried with mail() but couldn't reach any result. 我尝试使用mail()但无法达到任何结果。 The link is like below: 链接如下:

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

Can I make it with plain php or I have to it via javascript ? 我可以使用纯PHP制作它或者我必须通过javascript吗? Thank you in advance! 先感谢您!

cURL allows transfer of data across a wide variety of protocols, and is a very powerful system. cURL允许跨多种协议传输数据,是一个非常强大的系统。 It's widely used as a way to send data across websites, including things like API interaction and oAuth. 它被广泛用作跨网站发送数据的方式,包括API交互和oAuth等。 cURL is unrestricted in what it can do, from the basic HTTP request, to the more complex FTP upload or interaction with an authentication enclosed HTTPS site. 从基本的HTTP请求到更复杂的FTP上传或与身份验证封装的HTTPS站点的交互,cURL的功能不受限制。 We'll be looking at the simple difference between sending a GET and POST request and dealing with the returned response, as well as highlighting some useful parameters. 我们将关注发送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);

The basic idea behind the cURL functions is that you initialize a cURL session using the curl_init(), then you can set all your options for the transfer via the curl_setopt(), then you can execute the session with the curl_exec() and then you finish off your session using the curl_close(). cURL函数背后的基本思想是使用curl_init()初始化cURL会话,然后你可以通过curl_setopt()设置所有传输选项,然后你可以用curl_exec()执行会话然后你使用curl_close()完成会话。

You should use сURL. 你应该使用с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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM