Pregunta

I'm Trying to correctly use wp_remote_get in my wordpress plugin which uses our API. The API checks the request body for is_json, which returns 4 - aka JSON_ERROR_SYNTAX. Using cURL has the desired response. Here's my code:

$args = array(
    'body' => array('api_key' => 123),
    'timeout' => '5',
    'httpversion' => '1.0',
    'blocking' => true,
    'headers' => array(),
    'cookies' => array()
);

$result = wp_remote_request('https://myapi.endpoint/api/1.0/request', $args);
var_dump($result['body'])

var_dump: string(13) ""api_key=123""

whereas the var_dump of my cURL request is string(15) "{"api_key=123"}"

Any ideas as to what I am doing incorrectly?

¿Fue útil?

Solución

As Marc B stated, wp isn't sending json. You need to set the content type in the headers, and also send it as json:

$body = array('api_key' => 123);
$headers = array (
    'Accept' => 'application/json',
    'Content-Type' => 'application/json',
    'Content-Length' => strlen(json_encode($body)
);
$args = array(
    'method' => 'POST',
    'headers' => $headers,
    'body' => json_encode($body)
);
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top