PHP函数实现发送短信

时间:2013-02-12 07:05:01

标签: php

我正在尝试为这个项目实现发送SMS我正在使用PHP。注意:我并不是指向运营商和其他东西发送免费短信,我实际上联系了一家提供链接的SMS公司 www.smssender.com?username=myusername&pass=mypass&message=mymessage&recipient=phonenumber

PHP中的哪些功能可用于将此类请求发送到服务器API,并获得响应?这就是我想要的(伪代码):

function Sendsms(){
  add details to sting
  send url to sms server with the parameters
  get response and display
}

3 个答案:

答案 0 :(得分:2)

您想要执行以下操作(这是POST请求的示例) 我正在使用PHP的卷曲http://www.php.net/manual/en/function.curl-init.php

$url = 'http://domain.com/get-post.php';
$fields = array(
            'lname' => urlencode($last_name),
            'fname' => urlencode($first_name),
            'title' => urlencode($title),
            'company' => urlencode($institution),
            'age' => urlencode($age),
            'email' => urlencode($email),
            'phone' => urlencode($phone)
        );

//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);

对请求的响应位于变量$result

答案 1 :(得分:2)

看起来你正在做GET请求。你有没有看过php http_get函数?

<?php
$response = http_get("http://www.example.com/", array("timeout"=>1), $info);
print_r($info);
?>

来源:http://us2.php.net/manual/en/function.http-get.php

答案 2 :(得分:1)

function Sendsms()
{
   //add details to sting
     $url="www.smssender.comusername=myusername&pass=mypass&message=
      mymessage&recipient=phonenumber";

   //send url to sms server with the parameters
   $rsp = file_get_contents($url);

   //get response and display
   if( $res )
   {
        echo "sms successfully send";
   }
}