使用 PHP cURL 发送 SMS 文本消息的简单 GET 请求
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4188332/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Simple GET request with PHP cURL to send SMS text message
提问by JnBrymn
I'm creating quick web app that needs to send a php-created message from within php code. cURL is apparently the tool for the job, but I'm having difficulty understanding it enough to get it working.
我正在创建需要从 php 代码中发送 php 创建的消息的快速 Web 应用程序。cURL 显然是这项工作的工具,但我很难理解它以使其正常工作。
The documentation for the API I'm dealing with is here. In particular I want to use the simple GET-based sms notification documented here. The latter resource states that the GET API is simply:
我正在处理的 API 的文档在这里。特别是我想使用这里记录的简单的基于 GET 的短信通知。后一个资源指出 GET API 很简单:
http://sms2.cdyne.com/sms.svc/SimpleSMSsend?PhoneNumber={PHONENUMBER}&Message={MESSAGE}&LicenseKey={LICENSEKEY}
And indeed, if I type the following URL into a browser, I get the expected results:
事实上,如果我在浏览器中输入以下 URL,我会得到预期的结果:
http://sms2.cdyne.com/sms.svc/SimpleSMSsend?PhoneNumber=15362364325&Message=mymessage&LicenseKey=2134234882347139482314987123487
I am now trying to create the same affect within php. Here is my attempt:
我现在正在尝试在 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>
My other PHP webpages work fine, so I know php and apache are all set up correctly. But When I point my browser at the above page, I get no message on my phone. Can anybody show me what I'm doing wrong?
我的其他 PHP 网页工作正常,所以我知道 php 和 apache 都设置正确。但是当我将浏览器指向上述页面时,我的手机上没有任何消息。谁能告诉我我做错了什么?
Note: all numbers are faked... as you might have suspected.
注意:所有数字都是伪造的……正如您所怀疑的那样。
回答by Rytis
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.
你真的需要 CURL 吗?您只需使用 PHP 的file_get_contents($url)
,它将执行 GET 请求并返回响应值。
回答by PazsitZ
If there's no return output, probably the cURL fails. Check the error code of the returned resource to determine the cause of the error.
如果没有返回输出,则可能是 cURL 失败。检查返回资源的错误码,确定错误原因。
$result=curl_exec($ch);
$curlerrno = curl_errno($ch);
curl_close($ch);
print $curlerrno;
The error code list: libcurl-errors
错误代码列表:libcurl-errors
I advise to use cURL timeout settings too:
我建议也使用 cURL 超时设置:
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,5);
curl_setopt($ch,CURLOPT_TIMEOUT,5);
回答by Md Majadul Islam
Just replace it
PhoneNumber=$num
只需替换它
PhoneNumber=$num
curl_setopt($ch, CURLOPT_URL, "http://sms2.cdyne.com/sms.svc/SimpleSMSsend?PhoneNumber=".urlencode($num)."&Message=".urlencode($message)."&LicenseKey=2345987342583745349872");
curl_setopt($ch, CURLOPT_URL, " http://sms2.cdyne.com/sms.svc/SimpleSMSsend?PhoneNumber=".urlencode($num)."&Message=".urlencode($message)."&LicenseKey=2345987342583745349872" );
回答by PurplePilot
Assuming you are forming the URL correctly and as one comment says check it manually in a browser I am not sure where your data is going when it comes back so try
假设您正确地形成了 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 />";
Other things to try are
其他要尝试的事情是
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