0

我有一个使用 http 服务器远程工作的 sms android 应用程序,它需要获取一个形成的 url 请求,如下所示:

http://server.com:9090/sendsms?phone=123456789&text=foobar&pass=123456

当我在浏览器地址栏中键入该 URL 并按 Enter 键时,应用程序会发送短信。我是 curl 的新手,我不知道如何测试它,这是我到目前为止的代码:

  $phonenumber= '12321321321'
  $msgtext    = 'lorem ipsum'
  $pass       = '1234'

  $url = 'http://server.com:9090/sendsms?phone=' . urlencode($phonenumber) . '&text=' . urlencode($msgtext) . '&password=' . urlencode($pass);



  $curl = curl_init();
  curl_setopt_array($curl, array(
      CURLOPT_RETURNTRANSFER => 1,
      CURLOPT_URL => $url
  ));

所以我的问题是,代码是否正确?以及如何测试它?

4

2 回答 2

1

虽然这是一个简单的 GET,但我不能完全同意 hek2mgl。在很多情况下,当您必须处理超时、http 响应代码等时,这就是 cURL 的用途。

这是一个基本设置:

$handler = curl_init();
curl_setopt($handler, CURLOPT_URL, $url);
curl_setopt($handler, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handler, CURLOPT_FOLLOWLOCATION, true);
// curl_setopt($handler, CURLOPT_MAXREDIRS, 10); // optional
// curl_setopt($handler, CURLOPT_TIMEOUT, 10); // optional
$response = curl_exec($handler);
curl_close($handler);
于 2013-03-22T23:36:44.043 回答
0

如果您可以使用浏览器中的地址栏访问该 url,则它是一个 HTTP GET 请求。在 PHP 中最简单的方法是使用,file_get_contents()因为它也可以对 url 进行操作:

$url = 'http://server.com:9090/sendsms?phone=123456789&text=foobar&pass=123456';
$response = file_get_contents($url);

if($response === FALSE) {
    die('error sending sms');
}

// ... check the response message or whatever
...

当然你可以使用 curl 扩展,但是对于一个简单的 GET 请求,file_get_contents()将是最简单和最便携的解决方案。

于 2013-03-22T22:49:36.293 回答