{"id":506,"date":"2019-02-15T13:39:37","date_gmt":"2019-02-15T13:39:37","guid":{"rendered":"https:\/\/dapliw.org.ve\/?p=506"},"modified":"2019-02-15T13:39:37","modified_gmt":"2019-02-15T13:39:37","slug":"php-curl-y-crud-de-wp-rest-api-v2","status":"publish","type":"post","link":"https:\/\/saemas.com\/sitio_web\/php-curl-y-crud-de-wp-rest-api-v2\/","title":{"rendered":"Php &#8211; cURL y CRUD usuarios con Wp REST API v2"},"content":{"rendered":"<p>Para crear, leer, actualizar y eliminar usuarios (<strong>C<\/strong>create, <strong>R<\/strong>ead, <strong>U<\/strong>ptate y <strong>D<\/strong>elete) de WordPress utilizando la WP REST API v2 desde un programa cliente Php, seguimos los siguientes pasos:<\/p>\n<ul>\n<li><strong>C<\/strong>reate: <\/li>\n<pre>\n\/\/ Php Create\n$host = 'https:\/\/miDominio\/wp-json\/wp\/v2\/users';\n$data = array('username' => 'pedroperez', 'password' => 'pedro123', 'email' => 'pedro@gmail.com', 'first_name' => 'Pedro', 'last_name' => 'P\u00e9rez');\n$data_string = json_encode($data);\n$headers = array(\n    'Content-Type:application\/json',\n    'Content-Length: ' . strlen($data_string),\n    'Authorization: Basic '. base64_encode('miUsuarioWordpress:miClaveWordpress')\n);\n$ch = curl_init($host);\ncurl_setopt($ch, CURLOPT_HTTPHEADER, $headers);\ncurl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');  \ncurl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);\ncurl_setopt($ch, CURLOPT_RETURNTRANSFER,1);\n$result = curl_exec($ch);\ncurl_close($ch);  \necho($result);\n<\/pre>\n<li><strong>R<\/strong>ead:<\/li>\n<pre>\n\/\/ Php Read\n$host = 'https:\/\/miDominio\/wp-json\/wp\/v2\/users\/2';\n$headers = array(\n    'Content-Type:application\/json',\n    'Authorization: Basic '. base64_encode('miUsuarioWordpress:miClaveWordpress')\n);\n$ch = curl_init($host);\ncurl_setopt($ch, CURLOPT_HTTPHEADER, $headers);\ncurl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET'); \ncurl_setopt($ch, CURLOPT_RETURNTRANSFER,1);\n$result = curl_exec($ch);\ncurl_close($ch);  \necho($result);\n<\/pre>\n<li><strong>U<\/strong>pdate:<\/li>\n<pre>\n\/\/ Php Update\n$host = 'https:\/\/dapliw.org.ve\/wp-json\/wp\/v2\/users\/2';\n$data = array('email' => 'nuevoemail@gmail.com');\n$data_string = json_encode($data);\n$headers = array(\n    'Content-Type:application\/json',\n    'Content-Length: ' . strlen($data_string),\n    'Authorization: Basic '. base64_encode('miUsuarioWordpress:miClaveWordpress')\n);\n$ch = curl_init($host);\ncurl_setopt($ch, CURLOPT_HTTPHEADER, $headers);\ncurl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');  \ncurl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);\ncurl_setopt($ch, CURLOPT_RETURNTRANSFER,1);\n$result = curl_exec($ch);\ncurl_close($ch);  \necho($result);  \n<\/pre>\n<li><strong>D<\/strong>elete:<\/li>\n<pre>\n\/\/ Php Delete\n$host = 'https:\/\/dapliw.org.ve\/wp-json\/wp\/v2\/users\/2';\n$data = array('force' => true, 'reassign' => 1);\n$data_string = json_encode($data);\n$headers = array(\n    'Content-Type:application\/json',\n    'Content-Length: ' . strlen($data_string),\n    'Authorization: Basic '. base64_encode('miUsuarioWordpress:miClaveWordpress')\n);\n$ch = curl_init($host);\ncurl_setopt($ch, CURLOPT_HTTPHEADER, $headers);\ncurl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');  \ncurl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);\ncurl_setopt($ch, CURLOPT_RETURNTRANSFER,1);\n$result = curl_exec($ch);\ncurl_close($ch);  \necho($result);\n<\/pre>\n<li>En la operaci\u00f3n Delete se deben pasar en la variable $data los par\u00e1metros: &#8216;force&#8217; => true y &#8216;reassign&#8217; => idDelUsuarioAlCualSeLeAsignaranLosPostDelUsuarioQueSePretendeEliminar <\/li>\n<\/ul>\n<p>Fuente: <a href=\"https:\/\/decodecms.com\/como-usar-la-rest-api-de-wordpress\/\" rel=\"noopener noreferrer\" target=\"_blank\">https:\/\/decodecms.com\/como-usar-la-rest-api-de-wordpress\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 = &#8216;https:\/\/miDominio\/wp-json\/wp\/v2\/users&#8217;; $data = array(&#8216;username&#8217; => &#8216;pedroperez&#8217;, &#8216;password&#8217; => &#8216;pedro123&#8217;, &#8216;email&#8217; => &#8216;pedro@gmail.com&#8217;, &#8216;first_name&#8217; => &#8216;Pedro&#8217;, &#8216;last_name&#8217; => &#8216;P\u00e9rez&#8217;); $data_string = [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"material-hide-sections":[],"footnotes":""},"categories":[37],"tags":[38],"class_list":["post-506","post","type-post","status-publish","format-standard","hentry","category-php","tag-php"],"_links":{"self":[{"href":"https:\/\/saemas.com\/sitio_web\/wp-json\/wp\/v2\/posts\/506","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/saemas.com\/sitio_web\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/saemas.com\/sitio_web\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/saemas.com\/sitio_web\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/saemas.com\/sitio_web\/wp-json\/wp\/v2\/comments?post=506"}],"version-history":[{"count":0,"href":"https:\/\/saemas.com\/sitio_web\/wp-json\/wp\/v2\/posts\/506\/revisions"}],"wp:attachment":[{"href":"https:\/\/saemas.com\/sitio_web\/wp-json\/wp\/v2\/media?parent=506"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/saemas.com\/sitio_web\/wp-json\/wp\/v2\/categories?post=506"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/saemas.com\/sitio_web\/wp-json\/wp\/v2\/tags?post=506"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}