Bad Request while sending SMS from php

2k views Asked by At

I am using indiasms's api with curl to send sms

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

 ?>

Whenever I run this script I get

Bad Request

HTTP Error 400. The request is badly formed.

What i am doing wrong. ??

Update But when i run without curl it works fine

<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>

When i click this link it works normally

1

There are 1 answers

4
Korvo On BEST ANSWER

Supposedly the direct link works, because all headers needed for the request was sent.

I believe the lack of User-Agent: header or Connection: header

Try this:

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

If not work, try add, Connection: header:

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

If not work, try enconde your "message" before send:

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