Created
April 20, 2018 15:35
-
-
Save gwhitcher/ed858927d788d6d524f7be9d0107c6fc to your computer and use it in GitHub Desktop.
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 getWordpressPosts() { | |
$wordpressUrl = 'http://domain.com/'; | |
$endpoint = '/wp/v2/posts/'; | |
$vars = ''; | |
$curl = curl_init( $wordpressUrl . 'wp-json' . $endpoint.$vars ); | |
curl_setopt_array( $curl, array( | |
CURLOPT_RETURNTRANSFER => TRUE, | |
CURLOPT_HTTPAUTH => CURLAUTH_BASIC | |
) ); | |
$response = curl_exec( $curl ); | |
$values = json_decode($response, true); | |
$data = ''; | |
foreach($values as $key => $value) { | |
$data[] = [ | |
'title' => $value['title']['rendered'], | |
'url' => $value['link'] | |
]; | |
} | |
return $data; | |
} | |
function getWordpressProducts() { | |
$wordpressUrl = 'http://domain.com/'; | |
$endpoint = '/wp/v2/product/'; | |
$vars = ''; | |
$curl = curl_init( $wordpressUrl . 'wp-json' . $endpoint.$vars ); | |
curl_setopt_array( $curl, array( | |
CURLOPT_RETURNTRANSFER => TRUE, | |
CURLOPT_HTTPAUTH => CURLAUTH_BASIC | |
) ); | |
$response = curl_exec( $curl ); | |
//print_r($response); | |
$values = json_decode($response, true); | |
$data = ''; | |
foreach($values as $key => $value) { | |
$data[] = [ | |
'title' => $value['title']['rendered'], | |
'url' => $value['link'] | |
]; | |
} | |
return $data; | |
} | |
?> | |
<h2>Recent Posts</h2> | |
<ul> | |
<?php | |
$posts = getWordpressPosts(); | |
foreach($posts as $post) { | |
echo '<li><a href="'.$post['url'].'">'.$post['title'].'</a></li>'; | |
} | |
?> | |
</ul> | |
<h2>Recent Products</h2> | |
<ul> | |
<?php | |
$posts = getWordpressProducts(); | |
foreach($posts as $post) { | |
echo '<li><a href="'.$post['url'].'">'.$post['title'].'</a></li>'; | |
} | |
?> | |
</ul> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment