使用PHP cURL发送短信的简单GET请求

时间:2010-11-15 20:04:58

标签: php curl sms http-get

我正在创建需要从php代码中发送php创建的消息的快速Web应用程序。 cURL显然是工作的工具,但是我很难理解它以使其正常工作。

我正在处理的API的文档是here。特别是我想使用简单的基于GET的短信通知here。后一种资源表明GET API只是:

http://sms2.cdyne.com/sms.svc/SimpleSMSsend?PhoneNumber={PHONENUMBER}&Message={MESSAGE}&LicenseKey={LICENSEKEY}

事实上,如果我在浏览器中输入以下网址,我会得到预期的结果:

http://sms2.cdyne.com/sms.svc/SimpleSMSsend?PhoneNumber=15362364325&Message=mymessage&LicenseKey=2134234882347139482314987123487

我现在正试图在php中创建相同的效果。这是我的尝试:

<html>
<body>
<?php
$num = '13634859126';
$message = 'some swanky test message';

$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, "http://sms2.cdyne.com/sms.svc/SimpleSMSsend?PhoneNumber=".urlencode($num)."&Message=".urlencode($message)."&LicenseKey=2345987342583745349872");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
?>
</body>
</html>

我的其他PHP网页工作正常,所以我知道php和apache都设置正确。但当我将浏览器指向上面的页面时,我的手机上没有任何消息。谁能告诉我我做错了什么?

注意:所有数字都是假的......正如您可能怀疑的那样。

4 个答案:

答案 0 :(得分:10)

你真的需要CURL吗?您只需使用PHP的file_get_contents($url),它将执行GET请求并返回响应值。

答案 1 :(得分:1)

如果没有返回输出,可能cURL失败。 检查返回资源的错​​误代码以确定错误原因。

$result=curl_exec($ch);
$curlerrno = curl_errno($ch);
curl_close($ch);
print $curlerrno;

错误代码列表:libcurl-errors

我建议也使用cURL超时设置:

curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,5);
curl_setopt($ch,CURLOPT_TIMEOUT,5);

答案 2 :(得分:0)

假设您正在正确地形成URL,并且正如一条评论所说,在浏览器中手动检查它,我不确定数据返回时的位置,请尝试

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // tell the return not to go to the browser

     $output = curl_exec($ch); // point the data to a variable

     print "<br />"; // output the variable
     print $output;
     print "<br />";

其他要尝试的事情是

     curl_setopt($ch, CURLOPT_INTERFACE, "93.221.161.69"); // telling the remote system where to send the data back
     curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)"); // pretend you are IE/Mozilla in case the remote server expects it
     curl_setopt($ch, CURLOPT_POST, 1); // setting as a post

答案 3 :(得分:0)

只需更换它 ******中国= $ NUM

curl_setopt($ ch,CURLOPT_URL,&#34; http://sms2.cdyne.com/sms.svc/SimpleSMSsend?PhoneNumber=&#34; .urlencode($ num)。&#34;&amp; Message =&#34; .urlencode($ message)。& #34;&安培; LICENSEKEY = 2345987342583745349872&#34);