Skip to content

Instantly share code, notes, and snippets.

@luisgagocasas
Last active February 11, 2016 19:46
Show Gist options
  • Save luisgagocasas/d23ae241a4d6d94c3b25 to your computer and use it in GitHub Desktop.
Save luisgagocasas/d23ae241a4d6d94c3b25 to your computer and use it in GitHub Desktop.
Leer una API REST de wordpress desde PHP
<?php
//PRIMERA OPCION - funciones nativas php
//Optengo la API REST
$url = "http://localhost/wordpress/?page_id=2&json=1";
$data = file_get_contents($url);
$json = json_decode($data);
//Muestro los resultados
echo "<p>".$json->status."</p>";
echo "<p>".$json->page->id."</p>";
echo "<p>".$json->page->title."</p>";
//SEGUNDA OPCION - usando libreria curl
$url = "http://localhost/wordpress/?page_id=2&json=1";
$rCURL = curl_init();
curl_setopt($rCURL, CURLOPT_URL, $url);
curl_setopt($rCURL, CURLOPT_HEADER, 0);
curl_setopt($rCURL, CURLOPT_RETURNTRANSFER, 1);
$aData = curl_exec($rCURL);
curl_close($rCURL);
$json = json_decode ($aData);
//Muestro los resultados
echo "<p>".$json->status."</p>";
echo "<p>".$json->page->id."</p>";
echo "<p>".$json->page->title."</p>";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment