Created
December 30, 2016 09:08
-
-
Save celticwebdesign/7117e6a082ea5fb0ea3290bf01a86f4e to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
// // WordPress method - Start | |
// function get_json( $url, $arg ) { | |
// //GET remote site | |
// $response = wp_remote_get( $url, $arg ); | |
// //Checking for errors | |
// if ( is_wp_error( $response ) ) { | |
// return sprintf( 'Your URL %1s could not be retrieved', $url ); | |
// //GET only body | |
// $data = wp_remote_retrieve_body( $response ); | |
// } | |
// //return if no error | |
// if ( ! is_wp_error( $response ) ) { | |
// //Now, decode and return | |
// return json_decode( wp_remote_retrieve_body( $response ) ); | |
// } | |
// } | |
// // $url = rest_url('wp/v2/posts'); | |
// $url = 'http://www.XXX.net/wp-json/wp/v2/posts'; | |
// $headers = array ( | |
// 'Authorization' => 'Basic ' . base64_encode( 'username' . ':' . 'password' ), | |
// ); | |
// basic authentication | |
// $arg = array( | |
// 'method' => 'GET', | |
// 'timeout' => 45, | |
// 'redirection' => 5, | |
// 'httpversion' => '1.0', | |
// 'blocking' => true, | |
// 'headers' => $headers, | |
// 'cookies' => array () | |
// ); | |
// $posts = get_json($url, $arg); | |
// // pr($posts); | |
// foreach($posts as $post) { | |
// echo $post->title->rendered."<br />"; | |
// } | |
// // echo $posts[0]->content->rendered; | |
// // WordPress method - End | |
// // PHP method - Start | |
// function CallAPI($method, $url, $data = false) | |
// { | |
// $curl = curl_init(); | |
// switch ($method) | |
// { | |
// case "POST": | |
// curl_setopt($curl, CURLOPT_POST, 1); | |
// if ($data) | |
// curl_setopt($curl, CURLOPT_POSTFIELDS, $data); | |
// break; | |
// case "PUT": | |
// curl_setopt($curl, CURLOPT_PUT, 1); | |
// break; | |
// default: | |
// if ($data) | |
// $url = sprintf("%s?%s", $url, http_build_query($data)); | |
// } | |
// // Optional Authentication: | |
// curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); | |
// curl_setopt($curl, CURLOPT_USERPWD, "username:password"); | |
// basic authentication | |
// curl_setopt($curl, CURLOPT_URL, $url); | |
// curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); | |
// $result = curl_exec($curl); | |
// curl_close($curl); | |
// return $result; | |
// } | |
// $posts = CallAPI('GET', 'http://www.XXX.net/wp-json/wp/v2/posts'); | |
// // echo getType($posts); // string | |
// $posts = json_decode( $posts ); | |
// // echo getType($posts); // array | |
// // pr($posts); | |
// foreach($posts as $post) { | |
// echo $post->title->rendered."<br />"; | |
// } | |
// // PHP method - End | |
// UNIREST PHP methog - Start | |
// http://unirest.io/php.html | |
// you will need to download the Unirest files for this option | |
require_once 'Unirest.php'; | |
// and its required files | |
$headers = array( | |
'Accept' => 'application/json' | |
); | |
Unirest\Request::auth('username', 'password'); | |
// basic authentication | |
$response = Unirest\Request::get('http://www.XXX.net/wp-json/wp/v2/posts', $headers); | |
$response->code; // HTTP Status code | |
$response->headers; // Headers | |
$response->body; // Parsed body | |
$response->raw_body; // Unparsed body | |
// echo getType($response->raw_body); // string | |
$response = json_decode( $response->raw_body ); | |
// pr($response); | |
foreach($response as $respons) { | |
echo $respons->title->rendered."<br />"; | |
} | |
// UNIREST PHP method - End | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment