CURL:尝试调用http url时出现错误请求

tv6aics1  于 2022-11-13  发布在  其他
关注(0)|答案(4)|浏览(263)

我正在尝试使用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

出现以下错误:

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
lsmepo6l

lsmepo6l1#

您不能在URL中使用空格。您需要对以下字符串进行URL编码:

&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
返回一个字符串,其中除-_.之外的所有非字母数字字符都已替换为百分比(%)符号后跟两个十六进制数字和编码为加号的空格(+)号。它的编码方式与来自WWW表单的发布数据的编码方式相同,这与application/x-www-form-urlencoded media type中的编码方式相同。这与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
vhipe2zx

vhipe2zx2#

这可能是由于传递的邮件的URL中包含空格。请尝试使用urlencod

khbbv19g

khbbv19g3#

在url上使用urlencode(),因为有空格。另外,包含一个curl header也是一个很好的做法,如下所示:

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

vm0i2vca4#

此代码用于在注册完成后发送消息。我认为它对您有帮助

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

相关问题