简体   繁体   English

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

[英]Simple GET request with PHP cURL to send SMS text message

I'm creating quick web app that needs to send a php-created message from within php code. 我正在创建快速的Web应用程序,需要从PHP代码中发送php创建的消息。 cURL is apparently the tool for the job, but I'm having difficulty understanding it enough to get it working. cURL显然是这项工作的工具,但我很难理解它以使其正常工作。

The documentation for the API I'm dealing with is here . 我正在处理的API的文档就在这里 In particular I want to use the simple GET-based sms notification documented here . 特别是我想使用这里记录的简单的基于GET的短信通知。 The latter resource states that the GET API is simply: 后一种资源表明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. 我现在正试图在php中创建相同的效果。 Here is my attempt: 这是我的尝试:

<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. 我的其他PHP网页工作正常,所以我知道php和apache都设置正确。 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? 任何人都可以告诉我我做错了什么吗?

Note: all numbers are faked... as you might have suspected. 注意:所有数字都是伪造的...正如您可能怀疑的那样。

Do you really need CURL? 你真的需要CURL吗? You simply use PHP's file_get_contents($url) , which will do a GET request and will return response value. 您只需使用PHP的file_get_contents($url) ,它将执行GET请求并返回响应值。

If there's no return output, probably the cURL fails. 如果没有返回输出,则可能是cURL失败。 Check the error code of the returned resource to determine the cause of the error. 检查返回资源的错​​误代码以确定错误原因。

$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);

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

Just replace it 只需更换它
PhoneNumber=$num ******中国= $ 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” );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM