json 数据通过变量传递时,CURL 未返回正确的响应
CURL is not returning correct response when json data is passed through variable
$data
是保存 json 字符串的变量 {"clientId":"MyClientID","clientSecret":"MyClientSecret","script":"<?php\n echo \"Welcome to GLB Coding Club\";\n?>\n","stdin":"","language":"php","versionIndex":"2"}
.
正如您在下面第一行中看到的那样。我使用 json_encode()
将数组编码为 json
$data = json_encode(array("clientId"=>"MyClientID","clientSecret"=>"MyClientSecret","script"=> $this->input->post("script",true),"stdin"=>$this->input->post("stdin",true),"language"=>$this->input->post("language",true),"versionIndex"=>$this->input->post("versionIndex",true)));
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.jdoodle.com/v1/execute",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $data,
CURLOPT_HTTPHEADER => array("cache-control: no-cache","content-type: application/json"),
CURLOPT_SSL_VERIFYPEER => false
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
}
else {
echo $response;
}
jdoodle 没有返回正确的 $response
,而是返回要执行的语句。但是,如果我将 $data
替换为 CURLOPT_POSTFIELDS => $data
处的实际 json 字符串 {"clientId":"MyClientID","clientSecret":"MyClientSecret","script":"<?php\n echo \"Welcome to GLB Coding Club\";\n?>\n","stdin":"","language":"php","versionIndex":"2"}
,则 jdoodle 将返回正确的 $response
.
那么,干脆
json_encode(array("clientId"=>"MyClientID","clientSecret"=>"MyClientSecret","script"=> $this->input->post("script",true),"stdin"=>$this->input->post("stdin",true),"language"=>$this->input->post("language",true),"versionIndex"=>$this->input->post("versionIndex",true)))
没有生成您期望的 $data
字符串并给出正确的响应:
'{"clientId":"MyClientID","clientSecret":"MyClientSecret","script":"<?php\n echo \"Welcome to GLB Coding Club\";\n?>\n","stdin":"","language":"php","versionIndex":"2"}'
刚刚
echo $data;
exit(0);
在第一行之后检查形成 $data
字符串时有什么问题
我只是将 $this->input->post("script",true)
编辑为 html_entity_decode($this->input->post("script",true))
。
$data
是保存 json 字符串的变量 {"clientId":"MyClientID","clientSecret":"MyClientSecret","script":"<?php\n echo \"Welcome to GLB Coding Club\";\n?>\n","stdin":"","language":"php","versionIndex":"2"}
.
正如您在下面第一行中看到的那样。我使用 json_encode()
$data = json_encode(array("clientId"=>"MyClientID","clientSecret"=>"MyClientSecret","script"=> $this->input->post("script",true),"stdin"=>$this->input->post("stdin",true),"language"=>$this->input->post("language",true),"versionIndex"=>$this->input->post("versionIndex",true)));
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.jdoodle.com/v1/execute",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $data,
CURLOPT_HTTPHEADER => array("cache-control: no-cache","content-type: application/json"),
CURLOPT_SSL_VERIFYPEER => false
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
}
else {
echo $response;
}
jdoodle 没有返回正确的 $response
,而是返回要执行的语句。但是,如果我将 $data
替换为 CURLOPT_POSTFIELDS => $data
处的实际 json 字符串 {"clientId":"MyClientID","clientSecret":"MyClientSecret","script":"<?php\n echo \"Welcome to GLB Coding Club\";\n?>\n","stdin":"","language":"php","versionIndex":"2"}
,则 jdoodle 将返回正确的 $response
.
那么,干脆
json_encode(array("clientId"=>"MyClientID","clientSecret"=>"MyClientSecret","script"=> $this->input->post("script",true),"stdin"=>$this->input->post("stdin",true),"language"=>$this->input->post("language",true),"versionIndex"=>$this->input->post("versionIndex",true)))
没有生成您期望的 $data
字符串并给出正确的响应:
'{"clientId":"MyClientID","clientSecret":"MyClientSecret","script":"<?php\n echo \"Welcome to GLB Coding Club\";\n?>\n","stdin":"","language":"php","versionIndex":"2"}'
刚刚
echo $data;
exit(0);
在第一行之后检查形成 $data
字符串时有什么问题
我只是将 $this->input->post("script",true)
编辑为 html_entity_decode($this->input->post("script",true))
。