0

我正在尝试为我正在使用 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
}
4

3 回答 3

2

你想做如下的事情(这是一个 POST 请求的例子)我正在使用 PHP 的 curl 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

于 2013-02-12T07:07:51.433 回答
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

于 2013-02-12T07:09:58.183 回答
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";
   }
}
于 2013-02-12T07:20:09.150 回答