0

$data是保存 json string 的变量{"clientId":"MyClientID","clientSecret":"MyClientSecret","script":"<?php\n echo \"Welcome to GLB Coding Club\";\n?>\n","stdin":"","language":"php","versionIndex":"2"}

正如您在下面的第一行中看到的那样。我已使用将数组编码为 jsonjson_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实际的 json 字符串替换{"clientId":"MyClientID","clientSecret":"MyClientSecret","script":"<?php\n echo \"Welcome to GLB Coding Club\";\n?>\n","stdin":"","language":"php","versionIndex":"2"}CURLOPT_POSTFIELDS => $data那么 jdoodle 将返回正确的$response

4

2 回答 2

0

我只是编辑$this->input->post("script",true)html_entity_decode($this->input->post("script",true)).

于 2018-11-16T06:02:46.583 回答
0

那么,简单的

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在第一行之后检查形成字符串时出了什么问题

于 2018-11-02T20:07:25.083 回答