Jonah
Jonah

Reputation: 2115

CURL: BAD REQUEST error when trying to call http url

I am trying to call an sms api using a http url .I am trying to call the url using curl in php.I get a BAD REQUEST error .Please explain what I am doing wrong.

// create a new cURL resource
    $ch = curl_init();
    $string1 = "http://api.znisms.com/post/smsv3.asp?userid=alpesh67&apikey=74c6314840a16c5e7db00415a03181f7&message= Congratulation you have been successfully registered in the Placement Management System \n Email:".$email."\n Password:".$password."&senderid=PMS12345&sendto=".$contactno."";
    echo $string1;
    // set URL and other appropriate options
     curl_setopt($ch, CURLOPT_URL, $string1);
    // grab URL and pass it to the browser
    curl_exec($ch);
    //close cURL resource, and free up system resources
    curl_close($ch);
    //SMS END

I get the following error:

http://api.znisms.com/post/smsv3.asp?userid=alpesh67&apikey=74c6314840a16c5e7db00415a03181f7&message= Congratulation you have been successfully registered in the Placement Management System Email:[email protected] Password:123456789&senderid=PMS12345&sendto=9773396773
Bad Request

Upvotes: 2

Views: 37603

Answers (4)

Rakesh
Rakesh

Reputation: 11

THIS CODE FOR SEND Message after registration Completed. I think its helpful for you

$Res = mysqli_query($conn, $str);
if ($Res) 
{
    $last_Id=mysqli_insert_id($conn);
    $message="Registration Successful your Reg.Id is $last_Id You Get Confirmation Message if your Registration Accepeted.";
    $contact=$_POST['contact'];      
    $url = "http://api.znisms.com/post/smsv3.asp?userid=Test1234&apikey=74c6714840a16c5e7db00815a03181f7&message=".urlencode($message)."&senderid=KKK1278&sendto=".urlencode($contact)."";

    // create a new cURL resource
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_POST, false);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    curl_close($ch);

    // Process your response here
    echo $response;
    //header('location:../stdRegistration.php');
}

Upvotes: 1

Drahkar
Drahkar

Reputation: 1671

You can't use spaces in a URL. You need to url encode this string:

&message= Congratulation you have been successfully registered in the Placement Management System \n Email:".$email."\n Password:".$password."

http://php.net/manual/en/function.urlencode.php

Returns a string in which all non-alphanumeric characters except -_. have been replaced with a percent (%) sign followed by two hex digits and spaces encoded as plus (+) signs. It is encoded the same way that the posted data from a WWW form is encoded, that is the same way as in application/x-www-form-urlencoded media type. This differs from the » RFC 3986 encoding (see rawurlencode()) in that for historical reasons, spaces are encoded as plus (+) signs.

I would do something to the effect of:

// create a new cURL resource
    $ch = curl_init();
    $encoded_message = urlencode( "Congratulation you have been successfully registered in the Placement Management System \n Email:".$email."\n Password:".$password)
    $string1 = "http://api.znisms.com/post/smsv3.asp?userid=alpesh67&apikey=74c6314840a16c5e7db00415a03181f7&message=".$encoded_message."&senderid=PMS12345&sendto=".$contactno."";
    echo $string1;
    // set URL and other appropriate options
     curl_setopt($ch, CURLOPT_URL, $string1);
    // grab URL and pass it to the browser
    curl_exec($ch);
    //close cURL resource, and free up system resources
    curl_close($ch);
    //SMS END

Upvotes: 16

Dave Clarke
Dave Clarke

Reputation: 2696

Use urlencode() on the url as there are spaces. Also it is good practice to include a curl heaader as follows:

$headers = array("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.8) Gecko/20061025 Firefox/1.5.0.8");

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

Upvotes: 1

heyanshukla
heyanshukla

Reputation: 669

It may be due to the message being passed with spaces in the url. Try to urlencod

Upvotes: 1

Related Questions