从php发送短信时出现错误请求

时间:2014-11-12 12:26:42

标签: php curl sms-gateway

我正在使用indias的api和curl来发送短信

 <?php        

 $url = "http://12.23.54.18/api/smsapi.aspx?username=myusername&password=mypassword&to=7897015426&from=DEMOAB&message=Your message content.";            
 $curl = curl_init($url);
 curl_setopt_array($curl, array(
                    CURLOPT_RETURNTRANSFER => 1,
                    CURLOPT_URL => $url,
                    CURLOPT_USERAGENT => 'Codular Sample cURL Request'
                ));

 $resp = curl_exec($curl);
 curl_close($curl);

 ?>

每当我运行此脚本时,我都会

错误请求

HTTP错误400.请求格式错误。

我做错了什么。 ??

更新但是当我没有卷曲的情况下运行时它可以正常运行

<a href="http://12.23.54.18/api/smsapi.aspx?username=myusername&password=mypassword&to=7897015426&from=DEMOAB&message=Your message content.">Send Message</a>

当我点击此链接时,它可以正常工作

1 个答案:

答案 0 :(得分:3)

据说直接链接有效,因为请求所需的所有标头都已发送。

我认为缺少User-Agent:标题或Connection:标题

试试这个:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://12.23.54.18/api/smsapi.aspx?username=myusername&password=mypassword&to=7897015426&from=DEMOAB&message=Your message content.');
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);

$data = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

curl_close($ch);

echo $httpcode;

如果不起作用,请尝试添加Connection:标题:

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Connection: close'
));

如果不起作用,请尝试在发送之前加入“消息”:

$msg = rawurlencode('Your message content.');
curl_setopt($ch, CURLOPT_URL, 'http://12.23.54.18/api/smsapi.aspx?username=myusername&password=mypassword&to=7897015426&from=DEMOAB&message=' . $msg);