gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 08:15:09 24 4
gpt4 key购买 nike

我正在尝试使用 http url 调用短信 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

最佳答案

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

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.

我会做一些这样的事情:

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

关于php - CURL:尝试调用 http url 时出现 BAD REQUEST 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9683186/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com