Php – cURL y CRUD usuarios con Wp REST API v2

Para crear, leer, actualizar y eliminar usuarios (Ccreate, Read, Uptate y Delete) de WordPress utilizando la WP REST API v2 desde un programa cliente Php, seguimos los siguientes pasos:

  • Create:
  • // Php Create
    $host = 'https://miDominio/wp-json/wp/v2/users';
    $data = array('username' => 'pedroperez', 'password' => 'pedro123', 'email' => '[email protected]', 'first_name' => 'Pedro', 'last_name' => 'Pérez');
    $data_string = json_encode($data);
    $headers = array(
        'Content-Type:application/json',
        'Content-Length: ' . strlen($data_string),
        'Authorization: Basic '. base64_encode('miUsuarioWordpress:miClaveWordpress')
    );
    $ch = curl_init($host);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');  
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    $result = curl_exec($ch);
    curl_close($ch);  
    echo($result);
    
  • Read:
  • // Php Read
    $host = 'https://miDominio/wp-json/wp/v2/users/2';
    $headers = array(
        'Content-Type:application/json',
        'Authorization: Basic '. base64_encode('miUsuarioWordpress:miClaveWordpress')
    );
    $ch = curl_init($host);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET'); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    $result = curl_exec($ch);
    curl_close($ch);  
    echo($result);
    
  • Update:
  • // Php Update
    $host = 'https://dapliw.org.ve/wp-json/wp/v2/users/2';
    $data = array('email' => '[email protected]');
    $data_string = json_encode($data);
    $headers = array(
        'Content-Type:application/json',
        'Content-Length: ' . strlen($data_string),
        'Authorization: Basic '. base64_encode('miUsuarioWordpress:miClaveWordpress')
    );
    $ch = curl_init($host);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');  
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    $result = curl_exec($ch);
    curl_close($ch);  
    echo($result);  
    
  • Delete:
  • // Php Delete
    $host = 'https://dapliw.org.ve/wp-json/wp/v2/users/2';
    $data = array('force' => true, 'reassign' => 1);
    $data_string = json_encode($data);
    $headers = array(
        'Content-Type:application/json',
        'Content-Length: ' . strlen($data_string),
        'Authorization: Basic '. base64_encode('miUsuarioWordpress:miClaveWordpress')
    );
    $ch = curl_init($host);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');  
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    $result = curl_exec($ch);
    curl_close($ch);  
    echo($result);
    
  • En la operación Delete se deben pasar en la variable $data los parámetros: ‘force’ => true y ‘reassign’ => idDelUsuarioAlCualSeLeAsignaranLosPostDelUsuarioQueSePretendeEliminar

Fuente: https://decodecms.com/como-usar-la-rest-api-de-wordpress/

chevron_left
chevron_right

Dejar un comentario

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *

Comentario
Nombre
Correo electrónico
Web