CURL:尝试调用http url时出现BAD REQUESt错误

Jon*_*nah 1 php curl web

我正在尝试使用http url调用sms api.我正在尝试使用php中的curl调用url.我得到了一个BAD REQUEST错误.请解释我做错了什么.

// 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
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

http://api.znisms.com/post/smsv3.asp?userid=alpesh67&apikey=74c6314840a16c5e7db00415a03181f7&message= Congratulation you have been successfully registered in the Placement Management System Email:alpeshhi@gmail.com Password:123456789&senderid=PMS12345&sendto=9773396773
Bad Request
Run Code Online (Sandbox Code Playgroud)

Dra*_*kar 14

您不能在URL中使用空格.你需要url编码这个字符串:

&message= Congratulation you have been successfully registered in the Placement Management System \n Email:".$email."\n Password:".$password."
Run Code Online (Sandbox Code Playgroud)

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

返回一个字符串,其中除-_之外的所有非字母数字字符.已被替换为百分号(%),后跟两个十六进制数字和空格,编码为加号(+).它的编码方式与编码WWW表单中的发布数据的方式相同,这与application/x-www-form-urlencoded媒体类型的方式相同.这与»RFC 3986编码(参见rawurlencode())的不同之处在于,由于历史原因,空格被编码为加号(+).

我会做一些事情的效果:

// 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
Run Code Online (Sandbox Code Playgroud)