4

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

我正在处理的 API 的文档在这里。特别是我想使用这里记录的简单的基于 GET 的短信通知。后一个资源指出 GET API 很简单:

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

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

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

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

<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

4 回答 4

10

Do you really need CURL? You simply use PHP's file_get_contents($url), which will do a GET request and will return response value.

于 2010-11-15T20:29:22.493 回答
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);
于 2010-11-15T21:41:49.470 回答
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
于 2010-11-15T20:16:20.727 回答
0

只需替换它
PhoneNumber=$num

curl_setopt($ch, CURLOPT_URL, " http://sms2.cdyne.com/sms.svc/SimpleSMSsend?PhoneNumber= ".urlencode($num)."&Message=".urlencode($message)."&LicenseKey=2345987342583745349872" );

于 2018-01-28T04:45:40.533 回答