Last active
December 30, 2024 14:46
-
-
Save hheinsoee/c903a8627aa49bab1da3ebd6625e42a1 to your computer and use it in GitHub Desktop.
Wordpress website တွေ အတွက် api ကို သေသပ်စွာ ပြင်ဆင်ထားပါသည်
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function wp() | |
{ | |
return "https://wordpresssite.com";//ကြိုက်တဲ့ wp website ကိုသုံးနိုင်ပါတယ် | |
} | |
$get = new Wp(wp()); | |
if (isset($_GET['category'])) { | |
$result = $get->byCategory( | |
$_GET['category'], | |
array( | |
"page" => $_GET['page'] | |
) | |
); | |
} elseif (isset($_GET['id'])) { | |
$result = $get->single($_GET['id']); | |
} elseif (isset($_GET['search'])) { | |
$result = $get->search($_GET['search']); | |
} elseif (isset($_GET['categories'])) { | |
$result = $get->categories(); | |
} else { | |
$result = $get->posts(array( | |
"page" => $_GET['page'] | |
)); | |
} | |
echo json_encode($result, TRUE); | |
class Wp | |
{ | |
function __construct($wpUrl) | |
{ | |
$this->wpUrl = $wpUrl; | |
} | |
function index() | |
{ | |
$w = $this->wpUrl; | |
$endpoint = $w . '/wp-json'; | |
$query = [ | |
'_fields' => 'name,description', //_links,author,excerpt,link,date, | |
]; | |
return myFetcher($endpoint, $query); | |
} | |
function posts($request) | |
{ | |
$w = $this->wpUrl; | |
$page = $request['page']; | |
$endpoint = $w . '/?rest_route=/wp/v2/posts'; | |
$query = [ | |
'orderby' => 'date', | |
// //'offset' => $request->offset, | |
// 'per_page' => $request->per_page, | |
'per_page' => 5, | |
'page' => is_numeric($page) && $page > 0 ? $page : 1, | |
'_embed' => 'wp:featuredmedia', | |
'_fields' => 'id,title,date,_links,content,excerpt' //,author,excerpt,link,//contentကိုပုံရှာဖို့လိုရေ | |
]; | |
$data = myFetcher($endpoint, $query); | |
if (is_array($data)) { | |
$array = []; | |
foreach ($data as $post) { | |
$array[] = $post; | |
} | |
return formater('thumbnail', $array); | |
} else { | |
return $data; | |
} | |
} | |
function categories() | |
{ | |
$w = $this->wpUrl; | |
$endpoint = $w . '/?rest_route=/wp/v2/categories'; | |
$query = [ | |
'per_page' => 50, | |
'_fields' => 'id,count,name,slug', //_links | |
]; | |
return myFetcher($endpoint, $query); | |
} | |
function byCategory($id, $request) | |
{ | |
$w = $this->wpUrl; | |
$page = $request['page']; | |
$endpoint = $w . '/?rest_route=/wp/v2/posts'; | |
$query = [ | |
'categories' => $id, | |
'orderby' => 'date', | |
//'offset' => $request->offset, | |
'per_page' => 5, | |
'page' => is_numeric($page) && $page > 0 ? $page : 1, | |
'_embed' => 'wp:featuredmedia', | |
'_fields' => 'id,title,date,_links,content,excerpt' //_links,author,excerpt,link,date, | |
]; //re array | |
$data = myFetcher($endpoint, $query); | |
if (is_array($data)) { | |
$array = []; | |
foreach ($data as $post) { | |
$array[] = $post; | |
} | |
return formater('thumbnail', $array); | |
} else { | |
return $data; | |
} | |
} | |
function single($id) | |
{ | |
$w = $this->wpUrl; | |
$endpoint = $w . '/?rest_route=/wp/v2/posts/' . $id; | |
$query = [ | |
'_embed' => 'wp:featuredmedia', | |
'_fields' => 'id,content,title,date,_links', //_links,author,excerpt,link,date,author,link, | |
]; | |
$array = myFetcher($endpoint, $query); | |
return formater('full', $array); | |
// return myFetcher($endpoint, $query); | |
} | |
function search($word) | |
{ | |
$w = $this->wpUrl; | |
$page = 1; | |
$endpoint = $w . '/?rest_route=/wp/v2/search'; | |
$query = [ | |
'search' => $word, | |
'orderby' => 'date', | |
//'per_page' => 5, | |
'page' => is_numeric($page) && $page > 0 ? $page : 1, | |
'_embed' => 'wp:featuredmedia', | |
'_fields' => 'id,title,date' //,_links | |
]; | |
return myFetcher($endpoint, $query); | |
} | |
} | |
//////////////////////////////////////////////////////////////////////////// | |
function myFetcher($endpoint, $query) | |
{ | |
$query = http_build_query($query); | |
$opts = array( | |
'http' => array( | |
'method' => 'GET', | |
'ignore_errors' => true | |
), | |
"ssl" => array( | |
"verify_peer" => false, | |
"verify_peer_name" => false, | |
) | |
); | |
$context = stream_context_create($opts); | |
$result = @file_get_contents("{$endpoint}&{$query}", false, $context); | |
return json_decode($result); | |
} | |
function formater($how, $data) | |
{ | |
switch ($how) { | |
case 'thumbnail': | |
$response = []; | |
foreach ($data as $post) { | |
$thumbnail = isset($post->_embedded->{'wp:featuredmedia'}[0]->media_details->sizes->thumbnail->source_url) | |
? $post->_embedded->{'wp:featuredmedia'}[0]->media_details->sizes->thumbnail->source_url | |
: isset($post->_embedded) | |
? $post->_embedded->{'wp:featuredmedia'}[0]->source_url | |
: findAPhoto($post->content->rendered); | |
// $headers = @get_headers($thumbnail); | |
if (strpos($thumbnail, 'http') === false && $thumbnail) { | |
$thumbnail_url = wp() . $thumbnail; | |
} else { | |
$thumbnail_url = $thumbnail; | |
} | |
$response[] = array( | |
'id' => $post->id, | |
'date' => $post->date, | |
'title' => $post->title->rendered, | |
'thumbnail' => $thumbnail_url, | |
'excerpt' => $post->excerpt->rendered | |
); | |
} | |
return $response; | |
break; | |
case 'full': | |
$post = $data; | |
$feature_image = | |
isset($data->_embedded->{'wp:featuredmedia'}[0]->media_details->sizes->medium->source_url) | |
? $data->_embedded->{'wp:featuredmedia'}[0]->media_details->sizes->medium->source_url | |
: isset($post->_embedded) | |
? $post->_embedded->{'wp:featuredmedia'}[0]->source_url | |
: false; | |
// $headers = @get_headers($feature_image); | |
if (strpos($feature_image, 'http') === false && $feature_image) { | |
$feature_image_url = wp() . $feature_image; | |
} else { | |
$feature_image_url = $feature_image; | |
} | |
$response = array( | |
'id' => $post->id, | |
'date' => $post->date, | |
'title' => $post->title->rendered, | |
'content' => $post->content->rendered, | |
'feature_image' => $feature_image_url | |
); | |
return $response; | |
break; | |
default: | |
# code... | |
break; | |
} | |
} | |
function findAPhoto($thecontent) | |
{ | |
$doc = new DOMDocument('1.0'); | |
libxml_use_internal_errors(true); | |
$doc->loadHTML($thecontent); | |
libxml_clear_errors(); | |
$selector = new DOMXPath($doc); | |
// way 1 | |
if (count($selector->query("//img[@src]")) > 0) { | |
$result = $selector->query("//img[@src]"); | |
// foreach ($result as $node) { | |
// $link = $node->getAttribute('src'); | |
// } | |
return $result[0]->getAttribute('src'); | |
} else { | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment