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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9683186/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 07:24:55  来源:igfitidea点击:

CURL: BAD REQUESt error when trying to call http url

phpcurlweb

提问by Jonah

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.

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

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

回答by Drahkar

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

不能在 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

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.

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

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

回答by heyanshukla

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

这可能是由于消息传递时在 url 中带有空格。尝试 urlencod

回答by Dave Clarke

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

因为有空格,所以在 url 上使用 urlencode()。此外,包含如下卷曲标题也是一种很好的做法:

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

回答by Rakesh

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