如何将 curl 转换为 wp_remote_post()?
how to convert curl into wp_remote_post()?
我有一个这样的 cUrl:
curl -sX POST api.hackerrank.com/checker/submission.json -d 'source=print 1&lang=5&testcases=["1"]&api_key=yourapikeyfoobarbaz'
我尝试使用 postman 测试这个 curl,它给出了以下响应:
{
"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>';
}
?>
我得到 'bad request response'。我的代码出了什么问题?这扼杀了我的一天。任何帮助都是适当的。
试试这个代码。我正在使用 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();
我有一个这样的 cUrl:
curl -sX POST api.hackerrank.com/checker/submission.json -d 'source=print 1&lang=5&testcases=["1"]&api_key=yourapikeyfoobarbaz'
我尝试使用 postman 测试这个 curl,它给出了以下响应:
{
"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>';
}
?>
我得到 'bad request response'。我的代码出了什么问题?这扼杀了我的一天。任何帮助都是适当的。
试试这个代码。我正在使用 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();