先日cURLで書かれていたコードをwp_remote_postに変更したのでメモ。
cURL
$params = [
'xxx' => 'aaa'
'yyy' => 'bbb',
'zzz' => 'ccc',
];
$headers = [
'contentType: application/json; charset=utf-8'
];
$curl = curl_init();
curl_setopt( $curl, CURLOPT_CUSTOMREQUEST, 'POST' );
curl_setopt( $curl, CURLOPT_URL, 'https://xxxxxxxxx.com/' );
curl_setopt( $curl, CURLOPT_POSTFIELDS, json_encode( $params ) );
curl_setopt( $curl, CURLOPT_HTTPHEADER, $headers );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, true );
$response = curl_exec( $curl );
curl_close( $curl );
$result = json_decode( $response, true );
wp_remote_post
$params = [
'xxx' => 'aaa'
'yyy' => 'bbb',
'zzz' => 'ccc',
];
$data = wp_remote_post('https://xxxxxxxxx.com/', array(
'method' => 'POST',
'headers' => array(
'content-Type' => 'application/json; charset=utf-8'
),
'httpversion' => '1.0',
'sslverify' => false,
'body' => json_encode($params)
));
if( is_wp_error( $data ) ) {
return false;
}
$response = wp_remote_retrieve_body( $data );
$result = json_decode( $response, true );