[英]PHP cURL doesn’t send post data
I have this code:我有这个代码:
$obj = $_POST["myData"];
$curl = curl_init();
$url = "whatever";
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $obj,
CURLOPT_HTTPHEADER => array(
"Content-Type: application/json"
),
));
$response = curl_exec($curl);
The problem is that the curl does not send the request, unless I change the CURLOPT_POSTFIELDS => $obj
, for the entire string which is:问题是 curl 不发送请求,除非我更改
CURLOPT_POSTFIELDS => $obj
,整个字符串是:
{\"desSucursal\":\"423423\",
\"indPlacas\":\"S\",
\"horarios\":[],
\"idConvenio\":{\"idConvenio\":\"1262\"},
\"usrRegistro\":\"1645144\",
\"usuarioJefe\":{\"idUsuario\":\"1645534\"}
}
But the var $obj, has the same data, cause $_POST["myData"], is a variable post that it was sent from JS.但是 var $obj 具有相同的数据,因为 $_POST["myData"] 是一个变量 post,它是从 JS 发送的。
Does anybody know, what is the reason that if I put the entire string works, but if I put the variable which contains the same string doesn't work?有谁知道,如果我把整个字符串都工作,但是如果我把包含相同字符串的变量放不工作的原因是什么?
Looks like there are extra slashes in your $_POST["myData"]
variable.看起来您的
$_POST["myData"]
变量中有额外的斜杠。 You need to remove those:您需要删除这些:
$obj = stripslashes($_POST["myData"]);
Note that you need to validate and sanitize any values you're getting from $_POST
.请注意,您需要验证和清理您从
$_POST
获得的任何值。 You should not trust any incoming data as this is an open door for vulnerabilities.您不应该信任任何传入的数据,因为这是为漏洞敞开的大门。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.