Invio di stringhe JSON attraverso wp_remote_post()

26 ago 2016, 14:52:55
Visualizzazioni: 42.3K
Voti: 27

Sto costruendo un'integrazione con MailChimp e richiedono una chiamata POST con codice JSON.

No, sto usando questo codice che in realtà funziona:

$data = wp_remote_post($url, array(
    'headers'   => array('Content-Type' => 'application/json; charset=utf-8'),
    'body'      => json_encode($array_with_parameters),
    'method'    => 'POST'
));

Ma, restituisce un avviso PHP

Warning: http_build_query(): Il Parametro 1 dovrebbe essere un Array o un Oggetto. Valore non corretto fornito in ../wp-includes/Requests/Transport/cURL.php alla riga 507

Come evitarlo?

Ho provato a utilizzare semplicemente l'array semplice nell'indice 'body' ma MailChimp restituisce un errore di parsing JSON.

2
Commenti

Hai applicato questa patch al core? https://core.trac.wordpress.org/ticket/37700

Otto Otto
26 ago 2016 15:45:31

Interessante. Essendo un plugin commerciale, deve funzionare su qualsiasi installazione WP. Ma visto che sembra essere un bug di WP, per me va bene. Molte grazie!

a-coder a-coder
26 ago 2016 17:05:07
Tutte le risposte alla domanda 1
2
36

Prova a impostare il parametro data_format nella tua richiesta in questo modo:

$data = wp_remote_post($url, array(
    'headers'     => array('Content-Type' => 'application/json; charset=utf-8'),
    'body'        => json_encode($array_with_parameters),
    'method'      => 'POST',
    'data_format' => 'body',
));

Sembra che il formato possa essere impostato di default su query, nel qual caso WordPress tenta di formattare i dati usando http_build_query, il che ti sta creando problemi dato che hai già formattato il body come stringa. Ecco il controllo rilevante in wp-includes/class-http.php:

if (!empty($data)) {
    $data_format = $options['data_format'];

    if ($data_format === 'query') {
        $url = self::format_get($url, $data);
        $data = '';
    }
    elseif (!is_string($data)) {
        $data = http_build_query($data, null, '&');
    }
}

Dato che il tuo errore proviene dalla riga 507 di wp-includes/Requests/Transport/cURL.php, possiamo vedere che questa è la chiamata principale a http_build_query:

protected static function format_get($url, $data) {
    if (!empty($data)) {
        $url_parts = parse_url($url);
        if (empty($url_parts['query'])) {
            $query = $url_parts['query'] = '';
        }
        else {
            $query = $url_parts['query'];
        }

        $query .= '&' . http_build_query($data, null, '&');
        $query = trim($query, '&');

        if (empty($url_parts['query'])) {
            $url .= '?' . $query;
        }
        else {
            $url = str_replace($url_parts['query'], $query, $url);
        }
    }
    return $url;
}
31 ago 2018 22:12:29
Commenti

Salvavita! Risposta perfetta.

Suraj Lulla Suraj Lulla
11 apr 2022 07:13:32

Ora si trova nei file wp-includes/Requests/src/Requests.php e wp-includes/Requests/src/Transport/*.php, ma la logica è la stessa. Sembra che il valore body sia solo una convenzione, il codice verifica solo qualsiasi cosa che non sia query, ma attenersi a quel valore funziona ed è la soluzione più future-proof.

Walf Walf
10 mag 2023 07:29:03