Wordpress 如何将curl转换为wp_remote_post()?

Wordpress 如何将curl转换为wp_remote_post()?,wordpress,wordpress-rest-api,Wordpress,Wordpress Rest Api,我的卷发是这样的: curl -sX POST api.hackerrank.com/checker/submission.json -d 'source=print 1&lang=5&testcases=["1"]&api_key=yourapikeyfoobarbaz' 我尝试使用postman测试这种卷曲,它给出了以下响应: { "result": { "callback_url": "", "censored_compil

我的卷发是这样的:

curl -sX POST api.hackerrank.com/checker/submission.json -d 'source=print 1&lang=5&testcases=["1"]&api_key=yourapikeyfoobarbaz'
我尝试使用postman测试这种卷曲,它给出了以下响应:

{
    "result": {
        "callback_url": "",
        "censored_compile_message": "",
        "codechecker_hash": "run-ekZIvhTNLIHcNkY5XtsO",
        "compile_command": "g++-8 -g -Wno-unused-result -Wreturn-type -Wmain -Werror=return-type -Werror=main -pipe -O2 -std=c++11 `pkg-config --cflags jsoncpp` -I solution.cc -o solution  -lm -lpthread `pkg-config --libs jsoncpp` 1> compile.err 2>&1",
        "compilemessage": "",
        "error_code": 0,
        "hash": "1549522607-910640363",
        "loopback": null,
        "memory": [
            8495104
        ],
        "message": [
            "Success"
        ],
        "response_s3_path": "2019_02_07_06/BRW38CHf9hoewtILXZJgjNOib0zQyKaMxmqS6FAVT41n2PkdUl5c5bd6af84b425.31080922",
        "result": 0,
        "run_command": "",
        "server": "ip-10-10-172-250",
        "signal": [
            0
        ],
        "stderr": [
            false
        ],
        "stdout": [
            "Hello Wold!\n"
        ],
        "time": [
            0.00185
        ]
    }
}
现在我想将其转换为wp_remote_post,如下所示:

        <?php
        $url = 'http://api.hackerrank.com/checker/submission.json';

        $sourceCode = '#include<iostream> 
        using namespace std;
        int main()
        { 
            cout<<"Hello Wold!"<<endl;
            return 0;
        }';

       $params     = array(
    'method'   => 'POST',
    'timeout'  => 45,
    'blocking' => true,
    'headers'  => array(
        'Content-Type' => 'application/json'
    ),
    'body'     => '['.json_encode( array(
            'source'         => $sourceCode,
            'lang'     => 2,
            'testcases'    => array(1),
            'api_key'        => 'hackerrank|1012942-759|ad05befda57bc43f1358ebee988682e4cc7ecd02'
        ) ).']'
);
        $response =  wp_remote_post( $url, $params);

        if ( is_wp_error( $response ) ) {
            $error_message = $response->get_error_message();
            echo "Something went wrong: $error_message";
        } else {
            echo 'Response:<pre>';
            print_r( $response );
            echo '</pre>';
        }
    ?>

我收到了“错误的请求响应”。我的代码出了什么问题?这会毁了我的一天。任何帮助都是适当的。

请尝试此代码。我使用的是curl post而不是wp_remote_post

function test(){
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, 'api.hackerrank.com/checker/submission.json');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, "source=print 1&lang=5&testcases=[\"1\"]&api_key=hackerrank|1012942-759|ad05befda57bc43f1358ebee988682e4cc7ecd02");
        curl_setopt($ch, CURLOPT_POST, 1);

        $headers = array();
        $headers[] = 'Content-Type: application/x-www-form-urlencoded';
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

        $result = curl_exec($ch);
        if (curl_errno($ch)) {
            echo 'Error:' . curl_error($ch);
        }
        curl_close ($ch);

        print_r($result);
}

test();